Skip to content

Commit

Permalink
closes #21 Add sender parameter to Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sargue committed Sep 16, 2017
1 parent c3439c1 commit a4e7167
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 19 deletions.
70 changes: 66 additions & 4 deletions src/main/java/net/sargue/mailgun/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import net.sargue.mailgun.content.ContentConverter;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Holds the configuration parameters needed by the library. This is a mutable
Expand All @@ -19,7 +22,7 @@ public class Configuration {
private String apiUrl = "https://api.mailgun.net/v3";
private String domain;
private String apiKey;
private String from;
private MultivaluedMap<String,String> defaultParameters = new MultivaluedHashMap<>();

private List<Converter<?>> converters =
Collections.synchronizedList(new ArrayList<Converter<?>>());
Expand Down Expand Up @@ -60,7 +63,22 @@ public Configuration() {
public Configuration(String domain, String apiKey, String from) {
this.domain = domain;
this.apiKey = apiKey;
this.from = from;
from(from);
}

/**
* Creates a copy of this configuration.
*
* @return a copy of this configuration
*/
public Configuration copy() {
Configuration copy = new Configuration();
copy.apiUrl = apiUrl;
copy.domain = domain;
copy.apiKey = apiKey;
copy.defaultParameters = new MultivaluedHashMap<String,String>(defaultParameters);
copy.converters.addAll(converters);
return copy;
}

/**
Expand Down Expand Up @@ -93,12 +111,15 @@ public Configuration apiKey(String apiKey) {
* a full address with a name ({@code Emmet Brown <doc@delorean.com>}).
* The latter form can also be achieved using the
* {@link #from(String, String)} method.
* <p>
* Note that this value is treated as a single value as opposed to the
* general default parameters stored in this configuration.
*
* @param from the default sender address
* @return this configuration
*/
public Configuration from(String from) {
this.from = from;
defaultParameters.putSingle("from", from);
return this;
}

Expand Down Expand Up @@ -126,6 +147,36 @@ public Configuration apiUrl(String apiUrl) {
return this;
}

/**
* Adds a new value to the specified default parameter.
* <p>
* This is only used if the parameter is not specified when building
* the specific mail.
* <p>
* Please note that parameters are multivalued. This method adds a new
* value. To set a new value you need to clear the default parameter first.
*
* @param name the name of the parameter
* @param value the new value to add to the parameter
* @return this configuration
* @see #clearDefaultParameter(String)
*/
public Configuration addDefaultParameter(String name, String value) {
defaultParameters.add(name, value);
return this;
}

/**
* Removes all the values of the specified default parameter.
*
* @param name the name of the parameter
* @return this configuration
*/
public Configuration clearDefaultParameter(String name) {
defaultParameters.remove(name);
return this;
}

/**
* Returns the configured Mailgun domain.
*
Expand All @@ -150,7 +201,7 @@ public String apiKey() {
* @return the configured default sender address
*/
public String from() {
return from;
return defaultParameters.getFirst("from");
}

/**
Expand All @@ -162,6 +213,17 @@ public String apiUrl() {
return apiUrl;
}

/**
* Returns the internal map of default parameters.
*
* This is not a copy. Changes to this map are persistent.
*
* @return the internal map of default parameters
*/
public Map<String, List<String>> defaultParameters() {
return defaultParameters;
}

/**
* Registers a converter.
*
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/net/sargue/mailgun/MailForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MultivaluedMap;

import java.util.List;
import java.util.Map;

import static javax.ws.rs.core.MediaType.APPLICATION_FORM_URLENCODED_TYPE;

Expand All @@ -20,9 +24,13 @@ Entity<?> entity() {

@Override
void prepareSend() {
// if no "from" specified revert to configuration default
if (!form.asMap().containsKey("from") &&
configuration().from() != null)
form.param("from", configuration().from());
// apply default parameters
MultivaluedMap<String, String> parameters = form.asMap();
Map<String, List<String>> defaultParameters = configuration().defaultParameters();
for (String name : defaultParameters.keySet()) {
if (!parameters.containsKey(name)) {
parameters.addAll(name, defaultParameters.get(name));
}
}
}
}
14 changes: 11 additions & 3 deletions src/main/java/net/sargue/mailgun/MailMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

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

class MailMultipart extends Mail {
private FormDataMultiPart form = new FormDataMultiPart();
Expand All @@ -21,9 +23,15 @@ Entity<?> entity() {

@Override
void prepareSend() {
// if no "from" specified revert to configuration default
if (form.getField("from") == null)
form.field("from", configuration().from());
// apply default parameters
Map<String, List<String>> defaultParameters = configuration().defaultParameters();
for (String name : defaultParameters.keySet()) {
if (form.getField(name) == null) {
for (String value : defaultParameters.get(name)) {
form.field(name, value);
}
}
}
}

@Override
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/net/sargue/mailgun/test/BasicTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,64 @@ public void withCustomHeader() {
);
}

@Test
public void withDefaultParameter() {
stubFor(expectedBasicPost().willReturn(aResponse().withStatus(200)));

Configuration cfg = configuration
.copy()
.addDefaultParameter("h:sender", "from@default.com");

Response response = MailBuilder.using(cfg)
.to("marty@mcfly.com")
.subject("This is a plain text test")
.text("Hello world!")
.build()
.send();

assertTrue(response.isOk());
assertEquals(Response.ResponseType.OK,
response.responseType());
assertEquals(200, response.responseCode());

verifyMessageSent(
param("to", "marty@mcfly.com"),
param("subject", "This is a plain text test"),
param("text", "Hello world!"),
param("h:sender", "from@default.com")
);
}

@Test
public void withDefaultParameterOverridden() {
stubFor(expectedBasicPost().willReturn(aResponse().withStatus(200)));

Configuration cfg = configuration
.copy()
.addDefaultParameter("h:sender", "from@default.com");

Response response = MailBuilder.using(cfg)
.to("marty@mcfly.com")
.subject("This is a plain text test")
.text("Hello world!")
.parameter("h:sender",
"from@specific.com")
.build()
.send();

assertTrue(response.isOk());
assertEquals(Response.ResponseType.OK,
response.responseType());
assertEquals(200, response.responseCode());

verifyMessageSent(
param("to", "marty@mcfly.com"),
param("subject", "This is a plain text test"),
param("text", "Hello world!"),
param("h:sender", "from@specific.com")
);
}

@Test
public void withCustomFrom() {
stubFor(expectedBasicPost().willReturn(aResponse().withStatus(200)));
Expand Down
64 changes: 57 additions & 7 deletions src/test/java/net/sargue/mailgun/test/ConfigurationTests.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,89 @@
package net.sargue.mailgun.test;

import com.google.common.collect.Lists;
import net.sargue.mailgun.Configuration;
import org.junit.Test;

import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

public class ConfigurationTests {

public static final String FROM = "Doc Brown <doc@delorean.com>";

@Test
public void testConfigurationFrom() {
String from = "Doc Brown <doc@delorean.com>";
Configuration configuration = new Configuration().from(from);
assertEquals(from, configuration.from());
Configuration configuration = new Configuration().from(FROM);
assertEquals(FROM, configuration.from());
}

@Test
public void testConfigurationSplitFrom() {
Configuration configuration =
new Configuration().from("Doc Brown", "doc@delorean.com");
assertEquals("Doc Brown <doc@delorean.com>", configuration.from());
assertEquals(FROM, configuration.from());
}

@Test
public void testConfigurationConstructor() {
String domain = "example.com";
String key = "1234";
String from = "Doc Brown <doc@delorean.com>";
Configuration configuration = new Configuration(domain, key, from);
Configuration configuration = new Configuration(domain, key, FROM);
assertEquals(domain, configuration.domain());
assertEquals(key, configuration.apiKey());
assertEquals(from, configuration.from());
assertEquals(FROM, configuration.from());
}

@Test
public void testConfigurationURL() {
Configuration configuration = new Configuration().apiUrl("anotherURL");
assertEquals("anotherURL", configuration.apiUrl());
}

@Test
public void testFromDefault() {
Configuration configuration = new Configuration()
.from(FROM);
assertEquals(configuration.defaultParameters().get("from"),
Collections.singletonList(FROM));
}

@Test
public void testFromSingleValue() {
Configuration configuration = new Configuration()
.from("from1")
.from(FROM);
assertEquals(configuration.defaultParameters().get("from"),
Collections.singletonList(FROM));
}

@Test
public void testCustomDefault() {
Configuration configuration = new Configuration()
.addDefaultParameter("foo", "bar");
assertEquals(configuration.defaultParameters().get("foo"),
Collections.singletonList("bar"));
}

@Test
public void testCustomDefaultMultiValue() {
Configuration configuration = new Configuration()
.addDefaultParameter("foo", "bar")
.addDefaultParameter("foo", "bar2");
assertEquals(configuration.defaultParameters().get("foo"),
Lists.newArrayList("bar", "bar2"));
}

@Test
public void testResetDefault() {
Configuration configuration = new Configuration()
.addDefaultParameter("foo", "bar")
.addDefaultParameter("foo", "bar2")
.clearDefaultParameter("foo");
assertFalse(configuration.defaultParameters().containsKey("foo"));
assertNull(configuration.defaultParameters().get("foo"));
}
}
2 changes: 1 addition & 1 deletion src/test/java/net/sargue/mailgun/test/ContentTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public String toString(Date value) {
}
};

cfg.registerConverter(converter, Date.class);
cfg = cfg.registerConverter(converter, Date.class);

Body body = Body.builder(cfg)
.text(date)
Expand Down

0 comments on commit a4e7167

Please sign in to comment.