Search in sources :

Example 1 with WSConnection

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

the class TestODataUpdateExecution method helpExecute.

private String helpExecute(String query, final String resultXML, String expectedURL, int[] responseCode, TransformationMetadata metadata, int times) throws Exception {
    ODataExecutionFactory translator = new ODataExecutionFactory();
    translator.start();
    TranslationUtility utility = new TranslationUtility(metadata);
    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[0]));
    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(resultXML.getBytes());
            return in;
        }

        @Override
        public String getContentType() {
            return "application/xml";
        }
    };
    ArgumentCaptor<DataSource> payload = ArgumentCaptor.forClass(DataSource.class);
    Mockito.stub(dispatch.invoke(payload.capture())).toReturn(ds);
    UpdateExecution execution = translator.createUpdateExecution(cmd, context, utility.createRuntimeMetadata(), connection);
    execution.execute();
    ArgumentCaptor<String> endpoint = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> binding = ArgumentCaptor.forClass(String.class);
    Mockito.verify(connection, Mockito.times(times)).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
    Mockito.verify(dispatch, Mockito.times(times)).invoke(payload.capture());
    assertEquals(expectedURL, URLDecoder.decode(endpoint.getValue(), "utf-8"));
    if (payload.getAllValues() != null) {
        List<DataSource> listDS = payload.getAllValues();
        InputStream in = null;
        if (times > 1) {
            in = listDS.get(1).getInputStream();
        } else {
            in = listDS.get(0).getInputStream();
        }
        return new String(ObjectConverterUtil.convertToByteArray(in));
    }
    return "";
}
Also used : WSConnection(org.teiid.translator.WSConnection) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TranslationUtility(org.teiid.cdk.api.TranslationUtility) UpdateExecution(org.teiid.translator.UpdateExecution) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) List(java.util.List)

Example 2 with WSConnection

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

the class TestODataMetadataProcessor method createMetadata.

private static MetadataFactory createMetadata(final String file, final String schema, final String schemaNamespace) throws TranslatorException {
    ODataMetadataProcessor processor = new ODataMetadataProcessor() {

        protected XMLMetadata getSchema(WSConnection conn) throws TranslatorException {
            try {
                ClientODataDeserializerImpl deserializer = new ClientODataDeserializerImpl(false, ContentType.APPLICATION_XML);
                XMLMetadata metadata = deserializer.toMetadata(new FileInputStream(UnitTestUtil.getTestDataFile(file)));
                return metadata;
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    };
    Properties props = new Properties();
    props.setProperty("schemaNamespace", schemaNamespace);
    processor.setSchemaNamespace(schemaNamespace);
    MetadataFactory mf = new MetadataFactory("vdb", 1, schema, SystemMetadata.getInstance().getRuntimeTypeMap(), props, null);
    processor.process(mf, null);
    return mf;
}
Also used : WSConnection(org.teiid.translator.WSConnection) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) FileNotFoundException(java.io.FileNotFoundException) ClientODataDeserializerImpl(org.apache.olingo.client.core.serialization.ClientODataDeserializerImpl) XMLMetadata(org.apache.olingo.client.api.edm.xml.XMLMetadata) ClientCsdlXMLMetadata(org.apache.olingo.client.core.edm.ClientCsdlXMLMetadata) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 3 with WSConnection

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

the class TestWSTranslator method testStreaming.

@Test
public void testStreaming() throws Exception {
    WSExecutionFactory ef = new WSExecutionFactory();
    WSConnection mockConnection = Mockito.mock(WSConnection.class);
    MetadataFactory mf = new MetadataFactory("vdb", 1, "x", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    ef.getMetadata(mf, mockConnection);
    Procedure p = mf.getSchema().getProcedure(WSExecutionFactory.INVOKE_HTTP);
    assertEquals(7, p.getParameters().size());
    TransformationMetadata tm = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "vdb");
    RuntimeMetadataImpl rm = new RuntimeMetadataImpl(tm);
    Dispatch<Object> mockDispatch = mockDispatch();
    DataSource mock = Mockito.mock(DataSource.class);
    ByteArrayInputStream baos = new ByteArrayInputStream(new byte[100]);
    Mockito.stub(mock.getInputStream()).toReturn(baos);
    Mockito.stub(mockDispatch.invoke(Mockito.any(DataSource.class))).toReturn(mock);
    Mockito.stub(mockConnection.createDispatch(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Class.class), Mockito.any(Service.Mode.class))).toReturn(mockDispatch);
    CommandBuilder cb = new CommandBuilder(tm);
    Call call = (Call) cb.getCommand("call invokeHttp('GET', null, null, true)");
    BinaryWSProcedureExecution pe = new BinaryWSProcedureExecution(call, rm, Mockito.mock(ExecutionContext.class), ef, mockConnection);
    pe.execute();
    List<?> result = pe.getOutputParameterValues();
    Blob b = (Blob) result.get(0);
    assertEquals(100, ObjectConverterUtil.convertToByteArray(b.getBinaryStream()).length);
    try {
        ObjectConverterUtil.convertToByteArray(b.getBinaryStream());
        fail();
    } catch (SQLException e) {
    // should only be able to read once
    }
}
Also used : WSConnection(org.teiid.translator.WSConnection) Call(org.teiid.language.Call) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Blob(java.sql.Blob) SQLException(java.sql.SQLException) Properties(java.util.Properties) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) RuntimeMetadataImpl(org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl) Procedure(org.teiid.metadata.Procedure) CommandBuilder(org.teiid.cdk.CommandBuilder) Test(org.junit.Test)

Example 4 with WSConnection

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

the class TestWSTranslator method testPre81Procedure.

@Test
public void testPre81Procedure() throws Exception {
    WSExecutionFactory ef = new WSExecutionFactory();
    WSConnection mockConnection = Mockito.mock(WSConnection.class);
    MetadataFactory mf = new MetadataFactory("vdb", 1, "x", SystemMetadata.getInstance().getRuntimeTypeMap(), new Properties(), null);
    ef.getMetadata(mf, mockConnection);
    Procedure p = mf.getSchema().getProcedure(WSExecutionFactory.INVOKE_HTTP);
    assertEquals(7, p.getParameters().size());
    p.getParameters().remove(4);
    p.getParameters().remove(5);
    // designer treated the result as an out parameter
    p.getParameters().get(0).setType(Type.Out);
    p.getParameters().add(3, p.getParameters().remove(0));
    for (int i = 0; i < p.getParameters().size(); i++) {
        p.getParameters().get(i).setPosition(i + 1);
    }
    p = mf.getSchema().getProcedure("invoke");
    assertEquals(6, p.getParameters().size());
    p.getParameters().remove(5);
    // designer treated the result as an out parameter
    p.getParameters().get(0).setType(Type.Out);
    p.getParameters().add(p.getParameters().remove(0));
    for (int i = 0; i < p.getParameters().size(); i++) {
        p.getParameters().get(i).setPosition(i + 1);
    }
    TransformationMetadata tm = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "vdb");
    RuntimeMetadataImpl rm = new RuntimeMetadataImpl(tm);
    Dispatch<Object> mockDispatch = mockDispatch();
    DataSource source = Mockito.mock(DataSource.class);
    Mockito.stub(mockDispatch.invoke(Mockito.any(DataSource.class))).toReturn(source);
    Mockito.stub(mockConnection.createDispatch(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Class.class), Mockito.any(Service.Mode.class))).toReturn(mockDispatch);
    CommandBuilder cb = new CommandBuilder(tm);
    Call call = (Call) cb.getCommand("call invokeHttp('GET', null, null)");
    BinaryWSProcedureExecution pe = new BinaryWSProcedureExecution(call, rm, Mockito.mock(ExecutionContext.class), ef, mockConnection);
    pe.execute();
    pe.getOutputParameterValues();
    mockConnection = Mockito.mock(WSConnection.class);
    mockDispatch = Mockito.mock(Dispatch.class);
    StAXSource ssource = Mockito.mock(StAXSource.class);
    Mockito.stub(mockDispatch.invoke(Mockito.any(StAXSource.class))).toReturn(ssource);
    Mockito.stub(mockConnection.createDispatch(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(Class.class), Mockito.any(Service.Mode.class))).toReturn(mockDispatch);
    call = (Call) cb.getCommand("call invoke()");
    WSProcedureExecution wpe = new WSProcedureExecution(call, rm, Mockito.mock(ExecutionContext.class), ef, mockConnection);
    wpe.execute();
    wpe.getOutputParameterValues();
}
Also used : WSConnection(org.teiid.translator.WSConnection) Call(org.teiid.language.Call) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Dispatch(javax.xml.ws.Dispatch) StAXSource(javax.xml.transform.stax.StAXSource) Properties(java.util.Properties) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) RuntimeMetadataImpl(org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl) Procedure(org.teiid.metadata.Procedure) CommandBuilder(org.teiid.cdk.CommandBuilder) Test(org.junit.Test)

Example 5 with WSConnection

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

the class TestWSTranslatorMetadata method testMetadata.

@Test
public void testMetadata() throws Exception {
    WSExecutionFactory ef = new WSExecutionFactory();
    Properties props = new Properties();
    WSConnection mockConnection = Mockito.mock(WSConnection.class);
    Mockito.stub(mockConnection.getWsdl()).toReturn(new File(UnitTestUtil.getTestDataPath() + "/xquotes.wsdl").toURI().toURL());
    Mockito.stub(mockConnection.getServiceQName()).toReturn(new QName("http://www.xignite.com/services/", "XigniteQuotes"));
    Mockito.stub(mockConnection.getPortQName()).toReturn(new QName("http://www.xignite.com/services/", "XigniteQuotesSoap"));
    MetadataFactory mf = new MetadataFactory("vdb", 1, "x", SystemMetadata.getInstance().getRuntimeTypeMap(), props, null);
    ef.getMetadata(mf, mockConnection);
    assertEquals(36, mf.getSchema().getProcedures().size());
    TransformationMetadata tm = RealMetadataFactory.createTransformationMetadata(mf.asMetadataStore(), "vdb");
    RuntimeMetadataImpl rm = new RuntimeMetadataImpl(tm);
    Dispatch<Object> mockDispatch = Mockito.mock(Dispatch.class);
    StAXSource source = Mockito.mock(StAXSource.class);
    Mockito.stub(mockDispatch.invoke(Mockito.any(DataSource.class))).toReturn(source);
    Mockito.stub(mockConnection.createDispatch(Mockito.any(Class.class), Mockito.any(Service.Mode.class))).toReturn(mockDispatch);
    CommandBuilder cb = new CommandBuilder(tm);
    Call call = (Call) cb.getCommand("call GetFundQuote('<foo/>')");
    WSWSDLProcedureExecution wpe = new WSWSDLProcedureExecution(call, rm, Mockito.mock(ExecutionContext.class), ef, mockConnection);
    wpe.execute();
    wpe.getOutputParameterValues();
}
Also used : WSConnection(org.teiid.translator.WSConnection) Call(org.teiid.language.Call) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) QName(javax.xml.namespace.QName) StAXSource(javax.xml.transform.stax.StAXSource) Properties(java.util.Properties) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) RealMetadataFactory(org.teiid.query.unittest.RealMetadataFactory) MetadataFactory(org.teiid.metadata.MetadataFactory) RuntimeMetadataImpl(org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl) CommandBuilder(org.teiid.cdk.CommandBuilder) File(java.io.File) Test(org.junit.Test)

Aggregations

WSConnection (org.teiid.translator.WSConnection)13 DataSource (javax.activation.DataSource)11 ExecutionContext (org.teiid.translator.ExecutionContext)11 ByteArrayInputStream (java.io.ByteArrayInputStream)8 List (java.util.List)7 Properties (java.util.Properties)7 MetadataFactory (org.teiid.metadata.MetadataFactory)7 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 HashMap (java.util.HashMap)6 TranslationUtility (org.teiid.cdk.api.TranslationUtility)6 Command (org.teiid.language.Command)6 Test (org.junit.Test)5 CommandBuilder (org.teiid.cdk.CommandBuilder)5 RuntimeMetadataImpl (org.teiid.dqp.internal.datamgr.RuntimeMetadataImpl)5 Call (org.teiid.language.Call)5 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)5 File (java.io.File)3 StAXSource (javax.xml.transform.stax.StAXSource)3 Procedure (org.teiid.metadata.Procedure)3