Skip to content

Commit b0c05fe

Browse files
Merge pull request #150 from mailjet/transactional-email-builder-variables
fixes adding additional quotes to string variables, closes #149
2 parents 65db87b + 82b49ec commit b0c05fe

4 files changed

Lines changed: 90 additions & 37 deletions

File tree

src/main/java/com/mailjet/client/helpers/StringMapSerializer.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/main/java/com/mailjet/client/transactional/TransactionalEmail.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.mailjet.client.transactional;
22

3-
import com.google.gson.annotations.JsonAdapter;
43
import com.google.gson.annotations.SerializedName;
5-
import com.mailjet.client.helpers.StringMapSerializer;
64
import lombok.Builder;
75
import lombok.Singular;
86

@@ -179,20 +177,16 @@ public class TransactionalEmail {
179177

180178
/**
181179
* Additional email headers.
182-
* If the passed value type is not String, it will be serialized as a String with JSON representation of the passed object
183180
*/
184181
@Singular
185-
@JsonAdapter(StringMapSerializer.class)
186182
private Map<String, Object> headers;
187183

188184
/**
189185
* Adds variable used to modify the content of your email.
190186
* Specified as {var_name}:{var_value} pairs.
191-
* If the passed value type is not String, it will be serialized as a String with JSON representation of the passed object
192187
* Enter the information in the template text / HTML part by using the [[var:{var_name}]] format.
193188
* Equivalent of using X-MJ-Vars header through SMTP.
194189
*/
195190
@Singular
196-
@JsonAdapter(StringMapSerializer.class)
197191
private Map<String, Object> variables;
198192
}

src/test/java/com/mailjet/client/TransactionalEmailBuilderIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public void SendEmailsRequest_SendsMessage() throws MailjetException, IOExceptio
4545
.attachment(Attachment.fromFile(attachmentPath))
4646
.header("test-header-key", "test-value")
4747
.variable("test-vars-array", new String[] {"a", "b", "c"})
48+
.variable("test-vars-string", "abc")
49+
.variable("test-vars-primitive", 123)
4850
.customID("custom-id-value")
4951
.build();
5052

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.mailjet.client;
2+
3+
import com.mailjet.client.errors.MailjetException;
4+
import com.mailjet.client.resource.*;
5+
import com.mailjet.client.resource.sms.SmsSend;
6+
import com.mailjet.client.transactional.*;
7+
import com.mailjet.client.transactional.response.SendEmailsResponse;
8+
import okhttp3.mockwebserver.MockResponse;
9+
import okhttp3.mockwebserver.MockWebServer;
10+
import okhttp3.mockwebserver.RecordedRequest;
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
13+
import org.junit.AfterClass;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
import java.io.IOException;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertTrue;
23+
24+
public class TransactionalEmailBuilderTest {
25+
26+
private static MailjetClient client;
27+
private static MockWebServer mockWebServer;
28+
29+
@BeforeClass
30+
public static void initialize() throws IOException {
31+
mockWebServer = new MockWebServer();
32+
mockWebServer.start();
33+
34+
final ClientOptions clientOptions = ClientOptions
35+
.builder()
36+
.baseUrl(mockWebServer.url("/").toString())
37+
.apiSecretKey("secret-key")
38+
.apiKey("api-key")
39+
.bearerAccessToken("bearer-token")
40+
.build();
41+
42+
client = new MailjetClient(clientOptions);
43+
}
44+
45+
@Test
46+
public void transactionalEmail_WithVariables_ShouldCorrectlyPassVariables() throws MailjetException, InterruptedException {
47+
48+
// arrange
49+
String expectedBody = "{\"Messages\":[{\"Cc\":[],\"TrackClicks\":\"enabled\",\"Bcc\":[],\"Priority\":3,\"Headers\":{},\"From\":{\"Email\":\"xxxxxxxx@xxxxxxx.com\",\"Name\":\"xxxxxxxx xxxxxxxxxx\"},\"Attachments\":[],\"TemplateLanguage\":true,\"TrackOpens\":\"enabled\",\"Variables\":{\"E_DATE\":\"a string text\",\"E_ARRAY\":[\"val1\",\"val2\"],\"E_MAIL_ID\":123},\"CustomID\":\"8c0725fa-403c-496e-ac7e-xxxxxxxxx\",\"InlinedAttachments\":[],\"To\":[{\"Email\":\"xxxxxxxx@xxxxxxx.com\",\"Name\":\"xxxxxxxx xxxxxxxxxx\"}],\"TemplateID\":1234567}]}";
50+
51+
mockWebServer.enqueue(new MockResponse().setResponseCode(201).setBody("{}"));
52+
53+
Map<String, Object> variables = new HashMap<>();
54+
variables.put("E_DATE", "a string text");
55+
variables.put("E_ARRAY", new String[]{"val1", "val2"});
56+
variables.put("E_MAIL_ID", 123);
57+
58+
TransactionalEmail message = TransactionalEmail
59+
.builder()
60+
.to(new SendContact("xxxxxxxx@xxxxxxx.com", "xxxxxxxx xxxxxxxxxx"))
61+
.from(new SendContact("xxxxxxxx@xxxxxxx.com", "xxxxxxxx xxxxxxxxxx"))
62+
.templateID(1234567L)
63+
.templateLanguage(true)
64+
.priority(3)
65+
.customID("8c0725fa-403c-496e-ac7e-xxxxxxxxx")
66+
.variables(variables)
67+
.trackOpens(TrackOpens.ENABLED)
68+
.trackClicks(TrackClicks.ENABLED)
69+
.build();
70+
71+
SendEmailsRequest request = SendEmailsRequest.builder()
72+
.message(message)
73+
.build();
74+
75+
// act
76+
SendEmailsResponse response = request.sendWith(client);
77+
78+
// assert
79+
RecordedRequest recordedRequest = mockWebServer.takeRequest();
80+
81+
assertEquals(expectedBody, recordedRequest.getBody().readUtf8());
82+
}
83+
84+
@AfterClass
85+
public static void tearDown() throws IOException {
86+
mockWebServer.shutdown();
87+
}
88+
}

0 commit comments

Comments
 (0)