Search in sources :

Example 1 with ListLabelsResponse

use of com.google.api.services.gmail.model.ListLabelsResponse in project data-transfer-project by google.

the class GoogleMailService method getMigratedLabelId.

private String getMigratedLabelId() throws IOException {
    ListLabelsResponse response = gmail.users().labels().list(USER).execute();
    for (Label label : response.getLabels()) {
        if (label.getName().equals(LABEL)) {
            return label.getId();
        }
    }
    Label newLabel = new Label().setName(LABEL).setLabelListVisibility("labelShow").setMessageListVisibility("show");
    return gmail.users().labels().create(USER, newLabel).execute().getId();
}
Also used : Label(com.google.api.services.gmail.model.Label) ListLabelsResponse(com.google.api.services.gmail.model.ListLabelsResponse)

Example 2 with ListLabelsResponse

use of com.google.api.services.gmail.model.ListLabelsResponse in project wildfly-camel by wildfly-extras.

the class GoogleMailIntegrationTest method testGoogleMailLabels.

@Test
public void testGoogleMailLabels() 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 = "labels";
            // test route for create
            from("direct://CREATE").to("google-mail://" + pathPrefix + "/create");
            // 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 patch
            from("direct://PATCH").to("google-mail://" + pathPrefix + "/patch");
            // test route for update
            from("direct://UPDATE").to("google-mail://" + pathPrefix + "/update");
        }
    });
    try {
        camelctx.start();
        ProducerTemplate template = camelctx.createProducerTemplate();
        // using String message body for single parameter "userId"
        ListLabelsResponse labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
        final String testLabel = getClass().getSimpleName() + ".labels " + UUID.randomUUID().toString();
        String labelId;
        if (findLabel(labels, testLabel) == null) {
            Map<String, Object> headers = new HashMap<>();
            // parameter type is String
            headers.put("CamelGoogleMail.userId", CURRENT_USERID);
            Label label = new Label().setName(testLabel).setMessageListVisibility("show").setLabelListVisibility("labelShow");
            // parameter type is com.google.api.services.gmail.model.Label
            headers.put("CamelGoogleMail.content", label);
            Label result = template.requestBodyAndHeaders("direct://CREATE", null, headers, Label.class);
            Assert.assertNotNull("create result", result);
            labelId = result.getId();
        } else {
            labelId = findLabel(labels, testLabel).getId();
        }
        // using String message body for single parameter "userId"
        labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
        Assert.assertTrue(findLabel(labels, testLabel) != null);
        Map<String, Object> headers = new HashMap<>();
        // parameter type is String
        headers.put("CamelGoogleMail.userId", CURRENT_USERID);
        // parameter type is String
        headers.put("CamelGoogleMail.id", labelId);
        template.requestBodyAndHeaders("direct://DELETE", null, headers);
        // using String message body for single parameter "userId"
        labels = template.requestBody("direct://LIST", CURRENT_USERID, ListLabelsResponse.class);
        Assert.assertTrue(findLabel(labels, testLabel) == null);
    } 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) HashMap(java.util.HashMap) Label(com.google.api.services.gmail.model.Label) ListLabelsResponse(com.google.api.services.gmail.model.ListLabelsResponse) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with ListLabelsResponse

use of com.google.api.services.gmail.model.ListLabelsResponse in project data-transfer-project by google.

the class GoogleMailImporterTest method setUp.

@Before
public void setUp() throws IOException {
    Label label = new Label();
    label.setId(LABEL1);
    label.setName(LABEL1);
    labelsListResponse = new ListLabelsResponse().setLabels(Collections.singletonList(label));
    Monitor monitor = new Monitor() {
    };
    googleMailImporter = new GoogleMailImporter(googleCredentialFactory, gmail, monitor);
    executor = new FakeIdempotentImportExecutor();
    when(gmail.users()).thenReturn(users);
    when(users.messages()).thenReturn(messages);
    when(messages.insert(anyString(), any(Message.class))).thenReturn(insert);
    when(insert.execute()).thenReturn(new Message().setId("fooBar"));
    when(users.labels()).thenReturn(labels);
    when(labels.list(anyString())).thenReturn(labelsList);
    when(labelsList.execute()).thenReturn(labelsListResponse);
    when(labels.create(anyString(), any(Label.class))).thenReturn(labelsCreate);
    when(labelsCreate.execute()).thenReturn(label);
    verifyNoInteractions(googleCredentialFactory);
}
Also used : Monitor(org.datatransferproject.api.launcher.Monitor) Message(com.google.api.services.gmail.model.Message) FakeIdempotentImportExecutor(org.datatransferproject.test.types.FakeIdempotentImportExecutor) Label(com.google.api.services.gmail.model.Label) ListLabelsResponse(com.google.api.services.gmail.model.ListLabelsResponse) Before(org.junit.Before)

Aggregations

Label (com.google.api.services.gmail.model.Label)3 ListLabelsResponse (com.google.api.services.gmail.model.ListLabelsResponse)3 Message (com.google.api.services.gmail.model.Message)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 MessagingException (javax.mail.MessagingException)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 Monitor (org.datatransferproject.api.launcher.Monitor)1 FakeIdempotentImportExecutor (org.datatransferproject.test.types.FakeIdempotentImportExecutor)1 Before (org.junit.Before)1 Test (org.junit.Test)1