use of com.google.api.services.gmail.model.ListThreadsResponse in project wildfly-camel by wildfly-extras.
the class GoogleMailIntegrationTest method threads.
@SuppressWarnings("serial")
@Test
public void threads() throws Exception {
CamelContext camelctx = new DefaultCamelContext();
GoogleMailComponent gMailComponent = camelctx.getComponent("google-mail", GoogleMailComponent.class);
GoogleApiEnv.configure(gMailComponent.getConfiguration(), getClass(), LOG);
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
final String pathPrefix = "threads";
// test route for delete
from("direct://DELETE").to("google-mail://" + pathPrefix + "/delete");
// test route for get
from("direct://GET").to("google-mail://" + pathPrefix + "/get");
// test route for list
from("direct://LIST").to("google-mail://" + pathPrefix + "/list?inBody=userId");
// test route for modify
from("direct://MODIFY").to("google-mail://" + pathPrefix + "/modify");
// test route for trash
from("direct://TRASH").to("google-mail://" + pathPrefix + "/trash");
// test route for untrash
from("direct://UNTRASH").to("google-mail://" + pathPrefix + "/untrash");
}
});
try {
camelctx.start();
final String subject = getClass().getSimpleName() + ".threads " + UUID.randomUUID().toString();
ProducerTemplate template = camelctx.createProducerTemplate();
Message m1 = createThreadedMessage(null, subject, template);
final String threadId = m1.getThreadId();
createThreadedMessage(threadId, subject, template);
// using String message body for single parameter "userId"
ListThreadsResponse result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
Assert.assertNotNull("list result", result);
Assert.assertTrue(result.getThreads().size() > 0);
// ===== trash it ====
template.requestBodyAndHeaders("direct://TRASH", null, new HashMap<String, Object>() {
{
put("CamelGoogleMail.userId", CURRENT_USERID);
put("CamelGoogleMail.id", threadId);
}
});
// ==== Search for message we just trashed ====
result = template.requestBodyAndHeaders("direct://LIST", CURRENT_USERID, Collections.singletonMap("CamelGoogleMail.q", "subject:\"" + subject + "\""), ListThreadsResponse.class);
Assert.assertNotNull("list result", result);
Assert.assertTrue(result.getThreads() == null || result.getThreads().stream().noneMatch(t -> threadId.equals(t.getId())));
/* For some reason the thread deletion often needs some delay to succeed */
int attemptCount = 0;
for (; ; ) {
try {
template.requestBodyAndHeaders("direct://DELETE", null, new HashMap<String, Object>() {
{
put("CamelGoogleMail.userId", CURRENT_USERID);
put("CamelGoogleMail.id", threadId);
}
});
break;
/* success */
} catch (Exception e) {
if (attemptCount >= 5) {
throw e;
/* too many attempts */
} else {
/* retry */
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
}
}
attemptCount++;
}
}
} finally {
camelctx.stop();
}
}
Aggregations