use of org.teiid.translator.ws.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 "";
}
use of org.teiid.translator.ws.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() {
@Override
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;
}
use of org.teiid.translator.ws.WSConnection in project teiid by teiid.
the class TestSwaggerMetadataProcessor method getMetadata.
private static MetadataFactory getMetadata(SwaggerExecutionFactory ef, final String file) throws TranslatorException {
SwaggerMetadataProcessor processor = new SwaggerMetadataProcessor(ef) {
protected Swagger getSchema(WSConnection conn) throws TranslatorException {
File f = new File(file);
ObjectMapper objectMapper = new ObjectMapper();
try (FileInputStream fis = new FileInputStream(f)) {
JsonNode rootNode = objectMapper.readTree(fis);
return new SwaggerParser().read(rootNode, true);
} catch (IOException e) {
throw new TranslatorException(e);
}
}
};
processor.setPreferredProduces("application/json");
processor.setPreferredConsumes("application/json");
processor.setPreferredScheme("http");
Properties props = new Properties();
MetadataFactory mf = new MetadataFactory("vdb", 1, "swagger", SystemMetadata.getInstance().getRuntimeTypeMap(), props, null);
processor.process(mf, null);
return mf;
}
use of org.teiid.translator.ws.WSConnection in project teiid by teiid.
the class TestSwaggerQueryExecution method helpProcedureExecute.
private ProcedureExecution helpProcedureExecute(String query, final String resultJson, String expectedURL, int responseCode, boolean decode, String expectedMethod, String expectedInput, Map<String, Object> userHeaders) throws Exception {
userHeaders.put(MessageContext.HTTP_REQUEST_HEADERS, new HashMap<String, List<String>>());
userHeaders.put(WSConnection.STATUS_CODE, new Integer(responseCode));
userHeaders.put("Content-Type", Arrays.asList("application/json"));
SwaggerExecutionFactory translator = new SwaggerExecutionFactory();
translator.start();
TranslationUtility utility = new TranslationUtility(TestSwaggerMetadataProcessor.getTransformationMetadata(TestSwaggerMetadataProcessor.petstoreMetadata(translator), translator));
Command cmd = utility.parseCommand(query);
ExecutionContext context = Mockito.mock(ExecutionContext.class);
WSConnection connection = Mockito.mock(WSConnection.class);
Dispatch<DataSource> dispatch = Mockito.mock(Dispatch.class);
Mockito.stub(dispatch.getRequestContext()).toReturn(userHeaders);
Mockito.stub(dispatch.getResponseContext()).toReturn(userHeaders);
Mockito.stub(connection.createDispatch(Mockito.eq(HTTPBinding.HTTP_BINDING), Mockito.anyString(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE))).toReturn(dispatch);
DataSource outputDS = 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/json";
}
};
Mockito.stub(dispatch.invoke(Mockito.any(DataSource.class))).toReturn(outputDS);
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);
ArgumentCaptor<DataSource> input = ArgumentCaptor.forClass(DataSource.class);
Mockito.verify(connection).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
Mockito.verify(dispatch).invoke(input.capture());
assertEquals(expectedURL, decode ? URLDecoder.decode(endpoint.getValue(), "utf-8") : endpoint.getValue());
assertEquals(expectedMethod, dispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_METHOD));
if (expectedInput != null) {
assertEquals(expectedInput, ObjectConverterUtil.convertToString(input.getValue().getInputStream()));
}
return execution;
}
use of org.teiid.translator.ws.WSConnection in project teiid by teiid.
the class TestS3ProcedureExecution method testEndpoint.
@Test
public void testEndpoint() throws TranslatorException {
WSConnection conn = Mockito.mock(WSConnection.class);
S3ProcedureExecution s3ProcedureExecution = new S3ProcedureExecution(null, null, null, null, conn);
assertEquals("https://s3.z.amazonaws.com/y/x", s3ProcedureExecution.determineEndpoint("x", "y", "z"));
Mockito.stub(conn.getEndPoint()).toReturn("http://localhost:9000");
s3ProcedureExecution = new S3ProcedureExecution(null, null, null, null, conn);
assertEquals("http://localhost:9000/y/x", s3ProcedureExecution.determineEndpoint("x", "y", "z"));
}
Aggregations