Search in sources :

Example 1 with Response

use of org.lightcouch.Response in project camel by apache.

the class CouchDbConsumerIntegrationTest method testInsertsOnly.

@Test
public void testInsertsOnly() throws InterruptedException {
    to.expectedHeaderReceived(CouchDbConstants.HEADER_METHOD, "UPDATE");
    to.expectedMessageCount(1);
    JsonElement obj = new Gson().toJsonTree("{ \"randomString\" : \"" + UUID.randomUUID() + "\" }");
    Response resp = client.save(obj);
    client.remove(resp.getId(), resp.getRev());
    to.assertIsSatisfied();
}
Also used : Response(org.lightcouch.Response) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 2 with Response

use of org.lightcouch.Response in project camel by apache.

the class CouchDbProducerTest method testStringBodyIsConvertedToJsonTree.

@Test
public void testStringBodyIsConvertedToJsonTree() throws Exception {
    when(msg.getMandatoryBody()).thenReturn("{ \"name\" : \"coldplay\" }");
    when(client.save(anyObject())).thenAnswer(new Answer<Response>() {

        @Override
        public Response answer(InvocationOnMock invocation) throws Throwable {
            assertTrue(invocation.getArguments()[0].getClass() + " but wanted " + JsonElement.class, invocation.getArguments()[0] instanceof JsonElement);
            return new Response();
        }
    });
    producer.process(exchange);
    verify(client).save(any(JsonObject.class));
}
Also used : Response(org.lightcouch.Response) InvocationOnMock(org.mockito.invocation.InvocationOnMock) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 3 with Response

use of org.lightcouch.Response in project camel by apache.

the class CouchDbProducer method deleteJsonElement.

private Response deleteJsonElement(JsonElement json) {
    Response delete;
    if (json instanceof JsonObject) {
        JsonObject obj = (JsonObject) json;
        delete = couchClient.remove(obj);
    } else {
        delete = couchClient.remove(json);
    }
    return delete;
}
Also used : Response(org.lightcouch.Response) JsonObject(com.google.gson.JsonObject)

Example 4 with Response

use of org.lightcouch.Response in project camel by apache.

the class CouchDbProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    JsonElement json = getBodyAsJsonElement(exchange);
    String operation = exchange.getIn().getHeader(CouchDbConstants.HEADER_METHOD, String.class);
    if (ObjectHelper.isEmpty(operation)) {
        Response save = saveJsonElement(json);
        if (save == null) {
            throw new CouchDbException("Could not save document [unknown reason]", exchange);
        }
        if (log.isTraceEnabled()) {
            log.trace("Document saved [_id={}, _rev={}]", save.getId(), save.getRev());
        }
        exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_REV, save.getRev());
        exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_ID, save.getId());
    } else {
        if (operation.equalsIgnoreCase("DELETE")) {
            Response delete = deleteJsonElement(json);
            if (delete == null) {
                throw new CouchDbException("Could not delete document [unknown reason]", exchange);
            }
            if (log.isTraceEnabled()) {
                log.trace("Document saved [_id={}, _rev={}]", delete.getId(), delete.getRev());
            }
            exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_REV, delete.getRev());
            exchange.getIn().setHeader(CouchDbConstants.HEADER_DOC_ID, delete.getId());
        }
    }
}
Also used : Response(org.lightcouch.Response) JsonElement(com.google.gson.JsonElement)

Example 5 with Response

use of org.lightcouch.Response in project camel by apache.

the class CouchDbConsumerDeletesIntegrationTest method testDeletesOnly.

@Test
public void testDeletesOnly() throws InterruptedException {
    to.expectedHeaderReceived(CouchDbConstants.HEADER_METHOD, "DELETE");
    to.expectedMessageCount(1);
    JsonElement obj = new Gson().toJsonTree("{ \"randomString\" : \"" + UUID.randomUUID() + "\" }");
    Response resp = client.save(obj);
    client.remove(resp.getId(), resp.getRev());
    to.assertIsSatisfied();
}
Also used : Response(org.lightcouch.Response) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) Test(org.junit.Test)

Aggregations

Response (org.lightcouch.Response)6 JsonElement (com.google.gson.JsonElement)5 Test (org.junit.Test)4 Gson (com.google.gson.Gson)3 JsonObject (com.google.gson.JsonObject)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1