Skip to content

Commit 3c88306

Browse files
Merge pull request #142 from mailjet/add-template-reource-constants
Add template resource constants, adds template with utf8 symbols integration test
2 parents 7defcab + 14be451 commit 3c88306

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

src/main/java/com/mailjet/client/resource/Template.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class Template {
3535
public static String LIMIT = "Limit";
3636
public static String OFFSET = "Offset";
3737
public static String COUNTONLY = "CountOnly";
38+
public static String ISTEXTPARTGENERATIONENABLED = "IsTextPartGenerationEnabled";
39+
public static String LOCALE = "Locale";
3840

3941
}
4042

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.mailjet.client;
2+
3+
import com.mailjet.client.errors.MailjetException;
4+
import com.mailjet.client.resource.Template;
5+
import com.mailjet.client.resource.TemplateDetailcontent;
6+
import org.json.JSONArray;
7+
import org.json.JSONObject;
8+
import org.junit.Assert;
9+
import org.junit.FixMethodOrder;
10+
import org.junit.Test;
11+
import org.junit.runners.MethodSorters;
12+
13+
import java.util.UUID;
14+
15+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
16+
public class TemplateIT {
17+
18+
private static long templateId;
19+
private static MailjetClient mailjetClient = TestHelper.getClientV3();
20+
private static String utf8String = "ID_AÇÃO©℗®™";
21+
private static String mjmlContent =
22+
"<mjml>" +
23+
" <mj-body>" +
24+
" <mj-section>" +
25+
" <mj-column>" +
26+
" <mj-image width=\"100px\" src=\"/assets/img/logo-small.png\"></mj-image>" +
27+
" <mj-divider border-color=\"#F45E43\"></mj-divider>" +
28+
" <mj-text font-size=\"20px\" color=\"#F45E43\" font-family=\"helvetica\">Hello World" + utf8String +"</mj-text>" +
29+
" </mj-column>" +
30+
" </mj-section>" +
31+
" </mj-body>" +
32+
"</mjml>";
33+
34+
private static String htmlContent = "<h3>Dear passenger, welcome to Mailjet!</h3><br />May the delivery force be with you!" + utf8String;
35+
private static String textContent = "Dear passenger, welcome to Mailjet! May the delivery force be with you!" + utf8String;
36+
37+
@Test
38+
public void a_createTemplate() throws MailjetException {
39+
// arrange
40+
MailjetRequest mailjetRequest = new MailjetRequest(Template.resource)
41+
.property(Template.AUTHOR, "John Doe")
42+
.property(Template.CATEGORIES, new JSONArray("[\"demo-category\"]"))
43+
.property(Template.COPYRIGHT, "Mailjet")
44+
.property(Template.DESCRIPTION, "Used to send out promo codes.")
45+
.property(Template.EDITMODE, 1)
46+
.property(Template.ISSTARRED, false)
47+
.property(Template.ISTEXTPARTGENERATIONENABLED, true)
48+
//.property(Template.LOCALE, "en_US")
49+
.property(Template.NAME, "Promo Codes test template #" + UUID.randomUUID().toString())
50+
.property(Template.OWNERTYPE, "user")
51+
.property(Template.PRESETS, "string")
52+
.property(Template.PURPOSES, new JSONArray("[\"transactional\"]"));
53+
54+
// act
55+
MailjetResponse mailjetResponse = mailjetClient.post(mailjetRequest);
56+
57+
// assert
58+
Assert.assertEquals(201, mailjetResponse.getStatus());
59+
Assert.assertEquals(1, mailjetResponse.getCount());
60+
61+
templateId = mailjetResponse.getData().getJSONObject(0).getLong("ID");
62+
Assert.assertTrue(templateId > 0);
63+
}
64+
65+
@Test
66+
public void b_putTemplateContent() throws MailjetException {
67+
// arrange
68+
JSONObject templateHeaders = new JSONObject();
69+
templateHeaders.put("From", "test@mailjet.com");
70+
templateHeaders.put("Subject", "Test subject");
71+
templateHeaders.put("Reply-to", "test@mailjet.com");
72+
73+
MailjetRequest mailjetRequest = new MailjetRequest(TemplateDetailcontent.resource, templateId)
74+
.property(TemplateDetailcontent.HEADERS, templateHeaders)
75+
.property(TemplateDetailcontent.HTMLPART, htmlContent)
76+
.property(TemplateDetailcontent.MJMLCONTENT, mjmlContent)
77+
.property(TemplateDetailcontent.TEXTPART, textContent);
78+
79+
// act
80+
MailjetResponse mailjetResponse = mailjetClient.post(mailjetRequest);
81+
82+
// assert
83+
Assert.assertEquals(201, mailjetResponse.getStatus());
84+
Assert.assertEquals(1, mailjetResponse.getCount());
85+
86+
JSONObject templateData = mailjetResponse.getData().getJSONObject(0);
87+
Assert.assertEquals(htmlContent, templateData.getString(TemplateDetailcontent.HTMLPART));
88+
Assert.assertEquals(textContent, templateData.getString(TemplateDetailcontent.TEXTPART));
89+
Assert.assertEquals(mjmlContent, templateData.getString(TemplateDetailcontent.MJMLCONTENT));
90+
}
91+
92+
@Test
93+
public void c_getTemplateContent() throws MailjetException {
94+
// arrange
95+
MailjetRequest mailjetRequest = new MailjetRequest(TemplateDetailcontent.resource, templateId);
96+
97+
// act
98+
MailjetResponse mailjetResponse = mailjetClient.get(mailjetRequest);
99+
100+
// assert
101+
Assert.assertEquals(200, mailjetResponse.getStatus());
102+
Assert.assertEquals(1, mailjetResponse.getCount());
103+
104+
JSONObject templateData = mailjetResponse.getData().getJSONObject(0);
105+
Assert.assertEquals(htmlContent, templateData.getString(TemplateDetailcontent.HTMLPART));
106+
Assert.assertEquals(textContent, templateData.getString(TemplateDetailcontent.TEXTPART));
107+
Assert.assertEquals(mjmlContent, templateData.getString(TemplateDetailcontent.MJMLCONTENT));
108+
}
109+
110+
@Test
111+
public void d_deleteTemplate() throws MailjetException {
112+
// arrange
113+
MailjetRequest mailjetRequest = new MailjetRequest(Template.resource, templateId);
114+
115+
// act
116+
MailjetResponse mailjetResponse = mailjetClient.delete(mailjetRequest);
117+
118+
// assert
119+
Assert.assertEquals(204, mailjetResponse.getStatus());
120+
}
121+
}

0 commit comments

Comments
 (0)