Skip to content

Commit

Permalink
new methods to retrieve parameter values from a Mail instance
Browse files Browse the repository at this point in the history
  • Loading branch information
sargue committed May 30, 2018
1 parent ffd4471 commit 27d7cf1
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 30 deletions.
35 changes: 34 additions & 1 deletion src/main/java/net/sargue/mailgun/Mail.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import org.glassfish.jersey.client.JerseyClientBuilder;

import javax.ws.rs.client.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.InvocationCallback;
import java.util.List;

/**
* Representation of a Mailgun's mail request.
Expand All @@ -26,6 +30,35 @@ public static MailBuilder using(Configuration configuration) {
return new MailBuilder(configuration);
}

/**
* Retrieves the value of a given mail parameter. If there are multiple
* values the first one is returned. If the parameter hasn't been set
* null is returned.
*
* Can only be used on simple parameters (String). So don't use it on
* <i>attachment</i> for example. Doing so will throw a
* {@link IllegalStateException}.
*
* @param param the name of the parameter
* @return the first value of the parameter, if any, null otherwise
* @throws IllegalStateException if the parameter is not a simple (basic text) one
*/
public abstract String getFirstValue(String param);

/**
* Retrieves the values of a given mail parameter. If the parameter hasn't
* been set an empty list is returned.
*
* Can only be used on simple parameters (String). So don't use it on
* <i>attachment</i> for example. Doing so will throw a
* {@link IllegalStateException}.
*
* @param param the name of the parameter
* @return the list of values for the parameter or an empty list
* @throws IllegalStateException if the parameter is not a simple (basic text) one
*/
public abstract List<String> getValues(String param);

/**
* Sends the email.
* <p>
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/sargue/mailgun/MailForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ class MailForm extends Mail {
this.form = form;
}

@Override
public String getFirstValue(String param) {
return form.asMap().getFirst(param);
}

@Override
public List<String> getValues(String param) {
return form.asMap().get(param);
}

@Override
Entity<?> entity() {
return Entity.entity(form, APPLICATION_FORM_URLENCODED_TYPE);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/sargue/mailgun/MailMultipart.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.sargue.mailgun;

import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

Expand All @@ -16,6 +19,23 @@ class MailMultipart extends Mail {
this.form = form;
}

@Override
public String getFirstValue(String param) {
FormDataBodyPart bodyPart = form.getField(param);
return bodyPart == null ? null : bodyPart.getValue();
}

@Override
public List<String> getValues(String param) {
List<FormDataBodyPart> bodyParts = form.getFields(param);
if (bodyParts == null) return Collections.emptyList();

List<String> values = new ArrayList<>(bodyParts.size());
for (FormDataBodyPart bodyPart : bodyParts)
values.add(bodyPart.getValue());
return values;
}

@Override
Entity<?> entity() {
return Entity.entity(form, form.getMediaType());
Expand Down
74 changes: 45 additions & 29 deletions src/test/java/net/sargue/mailgun/test/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.github.tomakehurst.wiremock.client.MappingBuilder;
import com.github.tomakehurst.wiremock.client.RequestPatternBuilder;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import net.sargue.mailgun.Configuration;
import net.sargue.mailgun.MailBuilder;
import net.sargue.mailgun.MailRequestCallback;
import net.sargue.mailgun.Response;
import net.sargue.mailgun.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
Expand All @@ -23,6 +20,7 @@
import java.util.concurrent.TimeUnit;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.google.common.collect.Lists.newArrayList;
import static org.awaitility.Awaitility.await;
import static org.junit.Assert.*;

Expand Down Expand Up @@ -84,20 +82,22 @@ private void verifyMessageSent(NameValuePair... parameters) {
public void basicText() {
stubFor(expectedBasicPost().willReturn(aResponse().withStatus(200)));

Response response = MailBuilder.using(configuration)
.to("[email protected]")
.subject("This is a plain text test")
.text("Hello world!")
.build()
.send();
String to = "[email protected]";
Mail mail = MailBuilder.using(configuration)
.to(to)
.subject("This is a plain text test")
.text("Hello world!")
.build();
Response response = mail.send();

assertEquals(mail.getFirstValue("to"), to);
assertEquals(mail.getValues("to"), newArrayList(to));
assertTrue(response.isOk());
assertEquals(Response.ResponseType.OK,
response.responseType());
assertEquals(Response.ResponseType.OK, response.responseType());
assertEquals(200, response.responseCode());

verifyMessageSent(
param("to", "[email protected]"),
param("to", to),
param("subject", "This is a plain text test"),
param("text", "Hello world!")
);
Expand Down Expand Up @@ -234,27 +234,43 @@ public void withNullName() {
public void sendBasicTestMode() {
stubFor(expectedBasicPost().willReturn(aResponse().withStatus(200)));

Response response = MailBuilder.using(configuration)
.from("[email protected]")
.from("Random account", "[email protected]")
.to("[email protected]")
.cc("[email protected]")
.cc("Named CC", "[email protected]")
.bcc("[email protected]")
.bcc("Named BCC", "[email protected]")
.parameter("o:testmode", "yes")
.build()
.send();
String to = "[email protected]";
String cc1 = "[email protected]";
String cc2Name = "Named CC";
String cc2Mail = "[email protected]";
String bcc1 = "[email protected]";
String bcc2Name = "Named BCC";
String bcc2Mail = "[email protected]";
Mail mail = MailBuilder.using(configuration)
.from("[email protected]")
.from("Random account", "[email protected]")
.to(to)
.cc(cc1)
.cc(cc2Name, cc2Mail)
.bcc(bcc1)
.bcc(bcc2Name, bcc2Mail)
.parameter("o:testmode", "yes")
.build();
Response response = mail.send();

assertEquals(mail.getFirstValue("to"), to);
assertEquals(mail.getValues("to"), newArrayList(to));
assertEquals(mail.getFirstValue("cc"), cc1);
assertEquals(mail.getValues("cc"),
newArrayList(cc1, cc2Name + " <" + cc2Mail + ">"));
assertEquals(mail.getFirstValue("bcc"), bcc1);
assertEquals(mail.getValues("bcc"),
newArrayList(bcc1, bcc2Name + " <" + bcc2Mail + ">"));
assertTrue(response.isOk());

verifyMessageSent(
param("from", "[email protected]"),
param("from", mail("Random account", "[email protected]")),
param("to", "[email protected]"),
param("cc", "[email protected]"),
param("cc", mail("Named CC", "[email protected]")),
param("bcc", "[email protected]"),
param("bcc", mail("Named BCC", "[email protected]")),
param("to", to),
param("cc", cc1),
param("cc", mail(cc2Name, cc2Mail)),
param("bcc", bcc1),
param("bcc", mail(bcc2Name, bcc2Mail)),
param("o:testmode", "yes")
);
}
Expand Down

0 comments on commit 27d7cf1

Please sign in to comment.