|
| 1 | +package com.mailslurp.examples; |
| 2 | + |
| 3 | +import com.mailslurp.apis.EmailControllerApi; |
| 4 | +import com.mailslurp.apis.InboxControllerApi; |
| 5 | +import com.mailslurp.apis.WaitForControllerApi; |
| 6 | +import com.mailslurp.clients.ApiClient; |
| 7 | +import com.mailslurp.clients.ApiException; |
| 8 | +import com.mailslurp.clients.Configuration; |
| 9 | +import com.mailslurp.models.*; |
| 10 | +import org.junit.BeforeClass; |
| 11 | +import org.junit.Test; |
| 12 | +import org.openqa.selenium.WebDriver; |
| 13 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 14 | +import org.openqa.selenium.firefox.FirefoxOptions; |
| 15 | +import org.openqa.selenium.firefox.FirefoxProfile; |
| 16 | +import org.openqa.selenium.remote.CapabilityType; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.time.Duration; |
| 22 | +import java.time.temporal.ChronoUnit; |
| 23 | +import java.util.*; |
| 24 | + |
| 25 | +import static org.junit.Assert.*; |
| 26 | + |
| 27 | +public class MoreMethodsTest { |
| 28 | + |
| 29 | + // get a MailSlurp API Key free at https://app.mailslurp.com |
| 30 | + private static final String YOUR_API_KEY = System.getenv("API_KEY"); |
| 31 | + private static final Long TIMEOUT_MILLIS = 30000L; |
| 32 | + |
| 33 | + private static ApiClient mailslurpClient; |
| 34 | + private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 35 | + |
| 36 | + @BeforeClass |
| 37 | + public static void beforeAll() { |
| 38 | + assertNotNull(YOUR_API_KEY); |
| 39 | + // setup mailslurp |
| 40 | + mailslurpClient = Configuration.getDefaultApiClient(); |
| 41 | + mailslurpClient.setApiKey(YOUR_API_KEY); |
| 42 | + mailslurpClient.setConnectTimeout(TIMEOUT_MILLIS.intValue()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void canCreateInboxAndFetch() throws ApiException { |
| 47 | + InboxControllerApi inboxControllerApi = new InboxControllerApi(mailslurpClient); |
| 48 | + //<gen>java_create_inbox_with_tags_and_names |
| 49 | + // use names, description, and tags to identify an inbox |
| 50 | + String randomString = String.valueOf(new Random().nextLong()); |
| 51 | + String customName = "Test inbox " + randomString; |
| 52 | + String customDescription = "My custom description " + randomString; |
| 53 | + String customTag = "test-inbox-" + randomString; |
| 54 | + // create inbox with options so we can find it later |
| 55 | + CreateInboxDto options = new CreateInboxDto() |
| 56 | + .name(customName) |
| 57 | + .description(customDescription) |
| 58 | + .tags(Collections.singletonList(customTag)); |
| 59 | + InboxDto inbox = inboxControllerApi.createInboxWithOptions(options); |
| 60 | + //</gen> |
| 61 | + assertEquals(inbox.getName(), customName); |
| 62 | + assertEquals(inbox.getDescription(), customDescription); |
| 63 | + assertEquals(Objects.requireNonNull(inbox.getTags()).get(0), customTag); |
| 64 | + //<gen>java_fetch_inbox_by_name |
| 65 | + InboxByNameResult inboxByName = inboxControllerApi.getInboxByName(customName); |
| 66 | + assertEquals(inboxByName.getInboxId(), inbox.getId()); |
| 67 | + //</gen> |
| 68 | + //<gen>java_fetch_inbox_by_tags |
| 69 | + PageInboxProjection inboxSearchResult = inboxControllerApi.searchInboxes( |
| 70 | + new SearchInboxesOptions() |
| 71 | + .search(customTag) |
| 72 | + ); |
| 73 | + assertEquals(inboxSearchResult.getNumberOfElements(), Integer.valueOf(1)); |
| 74 | + assertEquals(inboxSearchResult.getContent().get(0).getId(), inbox.getId()); |
| 75 | + //</gen> |
| 76 | + String mySubject = "Test subject " + randomString; |
| 77 | + inboxControllerApi.sendEmailAndConfirm(inbox.getId(), new SendEmailOptions() |
| 78 | + .to(Collections.singletonList(inbox.getEmailAddress())) |
| 79 | + .subject(mySubject) |
| 80 | + ); |
| 81 | + //<gen>java_fetch_email_by_match |
| 82 | + WaitForControllerApi waitForControllerApi = new WaitForControllerApi(mailslurpClient); |
| 83 | + List<EmailPreview> matchingEmails = waitForControllerApi.waitFor(new WaitForConditions() |
| 84 | + .inboxId(inbox.getId()) |
| 85 | + .timeout(120000L) |
| 86 | + .count(1) |
| 87 | + .countType(WaitForConditions.CountTypeEnum.ATLEAST) |
| 88 | + .unreadOnly(true) |
| 89 | + .addMatchesItem(new MatchOption() |
| 90 | + .field(MatchOption.FieldEnum.SUBJECT) |
| 91 | + .should(MatchOption.ShouldEnum.CONTAIN) |
| 92 | + .value("Test subject") |
| 93 | + ) |
| 94 | + |
| 95 | + ); |
| 96 | + //</gen> |
| 97 | + assertEquals(matchingEmails.size(), 1); |
| 98 | + assertEquals(Objects.requireNonNull(matchingEmails).get(0).getFrom(), inbox.getEmailAddress()); |
| 99 | + //<gen>java_fetch_email_by_search |
| 100 | + EmailControllerApi emailController = new EmailControllerApi(mailslurpClient); |
| 101 | + PageEmailProjection emailSearch = emailController.searchEmails( |
| 102 | + new SearchEmailsOptions() |
| 103 | + .searchFilter("Test subject") |
| 104 | + ); |
| 105 | + //</gen> |
| 106 | + assertEquals(emailSearch.getNumberOfElements(), Integer.valueOf(1)); |
| 107 | + assertEquals(Objects.requireNonNull(emailSearch.getContent()).get(0).getFrom(), inbox.getEmailAddress()); |
| 108 | + UUID emailId = emailSearch.getContent().get(0).getId(); |
| 109 | + //<gen>java_get_email_by_id |
| 110 | + Email email = emailController.getEmail(emailId, false); |
| 111 | + assertEquals(email.getSubject(), mySubject); |
| 112 | + assertNotNull(email.getBody()); |
| 113 | + assertNotNull(email.getFrom()); |
| 114 | + assertNotNull(email.getAttachments()); |
| 115 | + //</gen> |
| 116 | + } |
| 117 | +} |
0 commit comments