Search in sources :

Example 1 with ListThreadsResponse

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();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) GoogleMailComponent(org.apache.camel.component.google.mail.GoogleMailComponent) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) Message(com.google.api.services.gmail.model.Message) MimeMessage(javax.mail.internet.MimeMessage) ListThreadsResponse(com.google.api.services.gmail.model.ListThreadsResponse) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ListThreadsResponse (com.google.api.services.gmail.model.ListThreadsResponse)1 Message (com.google.api.services.gmail.model.Message)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 MimeMessage (javax.mail.internet.MimeMessage)1 CamelContext (org.apache.camel.CamelContext)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 GoogleMailComponent (org.apache.camel.component.google.mail.GoogleMailComponent)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 Test (org.junit.Test)1