use of com.google.gson.JsonElement 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();
}
use of com.google.gson.JsonElement in project camel by apache.
the class CouchDbConsumerIntegrationTest method testMessages.
@Test
public void testMessages() throws InterruptedException {
final int messageCount = 100;
to.expectedMessageCount(messageCount);
// insert json manually into couch, camel will
// then pick that up and send to mock endpoint
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int k = 0; k < messageCount; k++) {
JsonElement obj = new Gson().toJsonTree("{ \"randomString\" : \"" + UUID.randomUUID() + "\" }");
client.save(obj);
}
}
});
t.start();
t.join();
to.assertIsSatisfied();
}
use of com.google.gson.JsonElement 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));
}
use of com.google.gson.JsonElement 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());
}
}
}
use of com.google.gson.JsonElement in project weixin-java-tools by chanjarster.
the class WxCpServiceImpl method tagGet.
@Override
public List<WxCpTag> tagGet() throws WxErrorException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/tag/list";
String responseContent = get(url, null);
JsonElement tmpJsonElement = Streams.parse(new JsonReader(new StringReader(responseContent)));
return WxCpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement.getAsJsonObject().get("taglist"), new TypeToken<List<WxCpTag>>() {
}.getType());
}
Aggregations