Search in sources :

Example 61 with Endpoint

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));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) ArrayList(java.util.ArrayList) Map(java.util.Map) Endpoint(org.apache.camel.Endpoint) Test(org.junit.Test)

Example 62 with Endpoint

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());
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Test(org.junit.Test)

Example 63 with Endpoint

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));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Map(java.util.Map) Test(org.junit.Test)

Example 64 with Endpoint

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));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Map(java.util.Map) Test(org.junit.Test)

Example 65 with Endpoint

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));
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Endpoint (org.apache.camel.Endpoint)615 Test (org.junit.Test)238 Exchange (org.apache.camel.Exchange)209 Producer (org.apache.camel.Producer)139 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)94 CamelContext (org.apache.camel.CamelContext)50 Processor (org.apache.camel.Processor)46 Message (org.apache.camel.Message)44 HashMap (java.util.HashMap)32 Map (java.util.Map)31 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)30 RouteBuilder (org.apache.camel.builder.RouteBuilder)28 Consumer (org.apache.camel.Consumer)27 File (java.io.File)26 ProducerTemplate (org.apache.camel.ProducerTemplate)23 Route (org.apache.camel.Route)21 EventDrivenConsumerRoute (org.apache.camel.impl.EventDrivenConsumerRoute)18 CountDownLatch (java.util.concurrent.CountDownLatch)16 DefaultExchange (org.apache.camel.impl.DefaultExchange)16 ArrayList (java.util.ArrayList)15