Java tutorial
/* * This file is a component of thundr, a software library from 3wks. * Read more: http://3wks.github.io/thundr/ * Copyright (C) 2014 3wks, <thundr@3wks.com.au> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.threewks.thundr.mailgun; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; import org.apache.commons.lang3.StringUtils; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import com.threewks.thundr.http.service.HttpService; import com.threewks.thundr.http.url.HttpServiceUrlConnection; import com.threewks.thundr.mail.MailBuilder; import com.threewks.thundr.mailgun.Log.LogItem; import com.threewks.thundr.request.RequestContainer; import com.threewks.thundr.request.ThreadLocalRequestContainer; import com.threewks.thundr.transformer.TransformerManager; import com.threewks.thundr.view.ViewResolverRegistry; import com.threewks.thundr.view.file.Disposition; import com.threewks.thundr.view.string.StringView; import com.threewks.thundr.view.string.StringViewResolver; public class MailgunImplIT { private Mailgun mailgun; private HttpService httpService; private ViewResolverRegistry viewResolverRegistry; private RequestContainer requestContainer = new ThreadLocalRequestContainer(); @Before public void before() { httpService = new HttpServiceUrlConnection(TransformerManager.createWithDefaults()); viewResolverRegistry = new ViewResolverRegistry(); viewResolverRegistry.addResolver(StringView.class, new StringViewResolver()); String key = String.format("%s%s%d", "key-8xv" + "qutmnj", "o3cchv5xv17rxltfcr8k6e", 7); mailgun = new MailgunImpl(viewResolverRegistry, requestContainer, httpService, "thundr-mailgun-test.mailgun.org", key); } @Ignore @Test public void shouldSendAnEmail() { // @formatter:off MailBuilder mailBuilder = mailgun.mail().to("test.3wks@gmail.com").cc("test.3wks@gmail.com") .bcc("test.3wks@gmail.com").from("test@thundr-mailgun-test.mailgun.org") .replyTo("test@thundr-mailgun-test.mailgun.org").subject("Test Mailgun") .body(new StringView("<html><body><h1>Yeah!</h1></body></html>")); // @formatter:on MessageSend result = mailgun.sendMailgun(mailBuilder); assertThat(result.id, is(not(nullValue()))); String expectedId = StringUtils.replaceChars(result.id, "<>", ""); String actualId = null; synchronized (this) { try { this.wait(5000); } catch (InterruptedException e) { } } Log log = mailgun.log(0, 20); for (LogItem item : log.items) { if (item.message_id.equals(expectedId)) { actualId = item.message_id; } } assertThat(actualId, is(expectedId)); } @Test public void shouldSendAnEmailWithAttachments() { // @formatter:off mailgun.mail().from("test@thundr-mailgun-test.mailgun.org").to("test.3wks@gmail.com") .subject("Test Mailgun") .body(new StringView("<html><body><h1>Yeah!</h1><img src=\"cid:inline-1\"/></body></html>")) .attach("inline-1", new StringView("Inline text"), Disposition.Inline) .attach("attached.txt", new StringView("Attached text"), Disposition.Attachment).send(); // @formatter:on } @Test public void shouldSendAnEmailUsingBuilder() { // @formatter:off mailgun.mail().from("test@thundr-mailgun-test.mailgun.org").to("test.3wks@gmail.com") .subject("Test Mailgun").body(new StringView("<html><body><h1>Yeah!</h1></body></html>")).send(); // @formatter:on } @Ignore @Test public void shouldReturnContentOnLogRequest() { Log log = mailgun.log(0, 10); assertThat(log.total_count, is(greaterThan(0))); } }