use of org.apache.camel.Endpoint in project camel by apache.
the class SqlGeneratedKeysTest method testRetrieveGeneratedKeysForBatch.
@Test
@SuppressWarnings("unchecked")
public void testRetrieveGeneratedKeysForBatch() throws Exception {
// first we create our exchange using the endpoint
Endpoint endpoint = context.getEndpoint("direct:batch");
Exchange exchange = endpoint.createExchange();
List<Object[]> payload = new ArrayList<Object[]>(4);
payload.add(new Object[] { "project x", "ASF", "new project x" });
payload.add(new Object[] { "project y", "ASF", "new project y" });
payload.add(new Object[] { "project z", "ASF", "new project z" });
payload.add(new Object[] { "project q", "ASF", "new project q" });
exchange.getIn().setBody(payload);
exchange.getIn().setHeader(SqlConstants.SQL_RETRIEVE_GENERATED_KEYS, true);
exchange.getIn().setHeader("foo", "123");
// now we send the exchange to the endpoint, and receives the response from Camel
Exchange out = template.send(endpoint, exchange);
// assertions of the response
assertNotNull(out);
assertNotNull(out.getOut());
assertNotNull(out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_DATA));
assertEquals("123", out.getOut().getHeader("foo"));
List<Map<String, Object>> generatedKeys = out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_DATA, List.class);
assertNotNull("out body could not be converted to a List - was: " + out.getOut().getBody(), generatedKeys);
// it seems not to work with Derby...
assertEquals(4, generatedKeys.size());
int id = 3;
for (Map<String, Object> row : generatedKeys) {
assertEquals("auto increment value should be " + id, Integer.valueOf(id++), row.get("ID"));
}
assertEquals("generated keys row count should be four", 4, out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_ROW_COUNT));
}
use of org.apache.camel.Endpoint in project camel by apache.
the class SqlGeneratedKeysTest method testGivenAnInvalidGeneratedColumnsHeaderThenAnExceptionIsThrown.
@Test
public void testGivenAnInvalidGeneratedColumnsHeaderThenAnExceptionIsThrown() throws Exception {
// first we create our exchange using the endpoint
Endpoint endpoint = context.getEndpoint("direct:insert");
Exchange exchange = endpoint.createExchange();
// then we set the SQL on the in body
exchange.getIn().setBody(new Object[] { "project x", "ASF", "new project" });
exchange.getIn().setHeader(SqlConstants.SQL_RETRIEVE_GENERATED_KEYS, true);
// set wrong data type for generated columns
exchange.getIn().setHeader(SqlConstants.SQL_GENERATED_COLUMNS, new Object[] {});
// now we send the exchange to the endpoint, and receives the response from Camel
template.send(endpoint, exchange);
assertTrue(exchange.isFailed());
}
use of org.apache.camel.Endpoint in project camel by apache.
the class SqlGeneratedKeysTest method testNoKeysForSelect.
@Test
@SuppressWarnings("unchecked")
public void testNoKeysForSelect() throws Exception {
// first we create our exchange using the endpoint
Endpoint endpoint = context.getEndpoint("direct:select");
Exchange exchange = endpoint.createExchange();
// then we set the SQL on the in body
exchange.getIn().setHeader(SqlConstants.SQL_RETRIEVE_GENERATED_KEYS, true);
// now we send the exchange to the endpoint, and receives the response from Camel
Exchange out = template.send(endpoint, exchange);
List<Map<String, Object>> result = out.getOut().getBody(List.class);
assertEquals("We should get 3 projects", 3, result.size());
List<Map<String, Object>> generatedKeys = out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_DATA, List.class);
assertEquals("We should not get any keys", 0, generatedKeys.size());
assertEquals("We should not get any keys", 0, out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_ROW_COUNT));
}
use of org.apache.camel.Endpoint in project camel by apache.
the class SqlGeneratedKeysTest method testRetrieveGeneratedKey.
@Test
@SuppressWarnings("unchecked")
public void testRetrieveGeneratedKey() throws Exception {
// first we create our exchange using the endpoint
Endpoint endpoint = context.getEndpoint("direct:insert");
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody(new Object[] { "project x", "ASF", "new project" });
exchange.getIn().setHeader(SqlConstants.SQL_RETRIEVE_GENERATED_KEYS, true);
// now we send the exchange to the endpoint, and receives the response from Camel
Exchange out = template.send(endpoint, exchange);
// assertions of the response
assertNotNull(out);
assertNotNull(out.getOut());
assertNotNull(out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_DATA));
List<Map<String, Object>> generatedKeys = out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_DATA, List.class);
assertNotNull("out body could not be converted to a List - was: " + out.getOut().getBody(), generatedKeys);
assertEquals(1, generatedKeys.get(0).size());
Map<String, Object> row = generatedKeys.get(0);
assertEquals("auto increment value should be 3", Integer.valueOf(3), row.get("ID"));
assertEquals("generated keys row count should be one", 1, out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_ROW_COUNT));
}
use of org.apache.camel.Endpoint in project camel by apache.
the class SqlGeneratedKeysTest method testRetrieveGeneratedKeyWithIntGeneratedColumns.
@Test
@SuppressWarnings("unchecked")
public void testRetrieveGeneratedKeyWithIntGeneratedColumns() throws Exception {
// first we create our exchange using the endpoint
Endpoint endpoint = context.getEndpoint("direct:insert");
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody(new Object[] { "project x", "ASF", "new project" });
exchange.getIn().setHeader(SqlConstants.SQL_RETRIEVE_GENERATED_KEYS, true);
exchange.getIn().setHeader(SqlConstants.SQL_GENERATED_COLUMNS, new int[] { 1 });
exchange.getIn().setHeader("foo", "123");
// now we send the exchange to the endpoint, and receives the response from Camel
Exchange out = template.send(endpoint, exchange);
// assertions of the response
assertNotNull(out);
assertNotNull(out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_DATA));
assertEquals("123", out.getOut().getHeader("foo"));
List<Map<String, Object>> generatedKeys = out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_DATA, List.class);
assertNotNull("out body could not be converted to a List - was: " + out.getOut().getBody(), generatedKeys);
assertEquals(1, generatedKeys.get(0).size());
Map<String, Object> row = generatedKeys.get(0);
assertEquals("auto increment value should be 3", Integer.valueOf(3), row.get("ID"));
assertEquals("generated keys row count should be one", 1, out.getOut().getHeader(SqlConstants.SQL_GENERATED_KEYS_ROW_COUNT));
}
Aggregations