use of org.teiid.translator.ProcedureExecution in project teiid by teiid.
the class TestSwaggerQueryExecution method testSimpleInputArray.
@Test
public void testSimpleInputArray() throws Exception {
String query = "exec findPetsByStatus(status=>('available',));";
String expectedURL = "http://petstore.swagger.io/v2/pet/findByStatus?status=available";
String response = "[\n" + " {\n" + "\"id\": 7,\n" + " \"category\": {\n" + " \"id\": 4,\n" + " \"name\": \"Lions\"\n" + " },\n" + " \"name\": \"Lion 1\",\n" + " \"photoUrls\": [\n" + " \"url1\",\n" + " \"url2\"\n" + " ],\n" + " \"tags\": [\n" + " {\n" + " \"id\": 1,\n" + " \"name\": \"tag1\"\n" + " },\n" + " {\n" + " \"id\": 2,\n" + " \"name\": \"tag2\"\n" + " }\n" + " ],\n" + " \"status\": \"available\"\n" + " },\n" + " {\n" + " \"id\": 10008,\n" + " \"category\": {\n" + " \"id\": 0,\n" + " \"name\": \"string\"\n" + " },\n" + " \"name\": \"doggie\",\n" + " \"photoUrls\": [\n" + " \"string\"\n" + " ],\n" + " \"tags\": [\n" + " {\n" + " \"id\": 0,\n" + " \"name\": \"string\"\n" + " }\n" + " ],\n" + " \"status\": \"available\"\n" + " }]";
ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200);
assertArrayEquals(new Object[] { 7L, 4L, "Lions", "Lion 1", new String[] { "url1", "url2" }, 1L, "tag1", "available" }, excution.next().toArray(new Object[8]));
assertArrayEquals(new Object[] { 7L, 4L, "Lions", "Lion 1", new String[] { "url1", "url2" }, 2L, "tag2", "available" }, excution.next().toArray(new Object[8]));
assertArrayEquals(new Object[] { 10008L, 0L, "string", "doggie", new String[] { "string" }, 0L, "string", "available" }, excution.next().toArray(new Object[8]));
assertNull(excution.next());
}
use of org.teiid.translator.ProcedureExecution in project teiid by teiid.
the class TestSwaggerQueryExecution method testOptionalParameter.
@Test
public void testOptionalParameter() throws Exception {
String query = "exec updatePet(name=>'fido');";
String expectedURL = "http://petstore.swagger.io/v2/pet";
String response = "";
ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200, true, "PUT", null, getHeaders());
}
use of org.teiid.translator.ProcedureExecution in project teiid by teiid.
the class TestSwaggerQueryExecution method testErrorResponse.
@Test(expected = TranslatorException.class)
public void testErrorResponse() throws Exception {
String query = "exec getPetById(petId=>687789);";
String expectedURL = "http://petstore.swagger.io/v2/pet/687789";
String response = "";
ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 400, true, "GET", null, getHeaders());
assertNull(excution.next());
}
use of org.teiid.translator.ProcedureExecution 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.ProcedureExecution in project teiid by teiid.
the class TestSwaggerQueryExecution method testMapReturn.
@Test
public void testMapReturn() throws Exception {
String query = "exec getInventory();";
String expectedURL = "http://petstore.swagger.io/v2/store/inventory";
String response = "{\n" + " \"sold\": 6,\n" + " \"string\": 7,\n" + " \"pending\": 62,\n" + " \"available\": 891,\n" + " \"Live\": 7,\n" + " \"fulfilled\": 1\n" + "}";
ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200, true, "GET", null, getHeaders());
assertArrayEquals(new Object[] { "sold", 6 }, excution.next().toArray(new Object[2]));
assertArrayEquals(new Object[] { "string", 7 }, excution.next().toArray(new Object[2]));
assertArrayEquals(new Object[] { "pending", 62 }, excution.next().toArray(new Object[2]));
assertArrayEquals(new Object[] { "available", 891 }, excution.next().toArray(new Object[2]));
assertArrayEquals(new Object[] { "Live", 7 }, excution.next().toArray(new Object[2]));
assertArrayEquals(new Object[] { "fulfilled", 1 }, excution.next().toArray(new Object[2]));
assertNull(excution.next());
}
Aggregations