Skip to content

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Nov 20, 2017
1 parent 4a4db41 commit 006ede5
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</developers>

<properties>
<postmark.version>0.9.2</postmark.version>
<postmark.version>0.9.3</postmark.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<junit.jupiter.version>5.0.0-M4</junit.jupiter.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class BaseMessage {
private ArrayList<HashMap<String, String>> attachments;

public BaseMessage() {
this.attachments = new ArrayList<>();
}

public BaseMessage(String from, String to, String subject, String htmlBody) {
Expand Down Expand Up @@ -218,6 +219,9 @@ public void addAttachment(String path) throws IOException {
}

public void addAttachment(HashMap<String, String> attachment) {
if (this.attachments == null) {
this.attachments = new ArrayList<>();
}
attachments.add(attachment);
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/java/base/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private enum DEFAULT_KEY_NAMES {

private final String propertyFile = ".properties";
public Properties properties;
private final Path testResourcesPath = Paths.get("src/test/resources/");
private final Path configFilePath = Paths.get("src/test/resources/" + propertyFile);

public String defaultApiToken;
Expand Down Expand Up @@ -63,6 +64,10 @@ public AccountApiClient getDefaultAccountApiClient() {
return Postmark.getAccountApiClient(defaultAccountToken);
}

public String getDefaultFilePath() {
return testResourcesPath.toString() + "/test.pdf";
}

private void initConfingFromFile() {

InputStream in = Postmark.class.getClassLoader().getResourceAsStream(propertyFile);
Expand Down
49 changes: 49 additions & 0 deletions src/test/java/integration/DomainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package integration;

import base.BaseTest;
import com.wildbit.java.postmark.client.AccountApiClient;
import com.wildbit.java.postmark.client.ApiClient;
import com.wildbit.java.postmark.client.Parameters;
import com.wildbit.java.postmark.client.data.model.domains.DomainDetails;
import com.wildbit.java.postmark.client.data.model.domains.Domains;
import com.wildbit.java.postmark.client.data.model.templates.*;
import com.wildbit.java.postmark.client.exception.PostmarkException;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Created by bash on 11/14/17.
*/
public class DomainTest extends BaseTest {

AccountApiClient client = getDefaultAccountApiClient();

@Test
void list() throws PostmarkException, IOException {
Domains domains = client.getDomains(Parameters.init().build("count",6).build("offset",0));
assertTrue(domains.getDomains().size() > 0);
}

@Test
void listById() throws PostmarkException, IOException {
Domains domains = client.getDomains(Parameters.init().build("count",6).build("offset",0));
Integer domainId = domains.getDomains().get(0).getId();
DomainDetails domainDetails = client.getDomainDetails(domainId);
assertNotNull(domainDetails.getDkimTextValue());
}

@Test
void verifySPF() throws PostmarkException, IOException {
Domains domains = client.getDomains(Parameters.init().build("count",6).build("offset",0));
Integer domainId = domains.getDomains().get(0).getId();
String response = client.verifyDomainSPF(domainId);
assertNotNull(response);
}

}
8 changes: 8 additions & 0 deletions src/test/java/integration/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.wildbit.java.postmark.client.ApiClient;
import com.wildbit.java.postmark.client.data.model.message.Message;
import com.wildbit.java.postmark.client.data.model.message.MessageResponse;
import com.wildbit.java.postmark.client.exception.InvalidAPIKeyException;
import com.wildbit.java.postmark.client.exception.InvalidMessageException;
import com.wildbit.java.postmark.client.exception.PostmarkException;

Expand Down Expand Up @@ -36,4 +37,11 @@ void invalidMessagetoSend() throws PostmarkException, IOException {
Throwable exception = assertThrows(InvalidMessageException.class, () -> client.deliverMessage(message));
assertEquals("Zero recipients specified", exception.getMessage());
}

@Test
void invalidApiToken() throws PostmarkException, IOException {
ApiClient client = Postmark.getApiClient("1991892", true);
Message message = new Message("[email protected]", "[email protected]", "Hello from Postmark!", "Hello body");
assertThrows(InvalidAPIKeyException.class, () -> client.deliverMessage(message));
}
}
42 changes: 42 additions & 0 deletions src/test/java/integration/SendersTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package integration;

import base.BaseTest;
import com.wildbit.java.postmark.client.AccountApiClient;
import com.wildbit.java.postmark.client.Parameters;
import com.wildbit.java.postmark.client.data.model.domains.DomainDetails;
import com.wildbit.java.postmark.client.data.model.domains.Domains;
import com.wildbit.java.postmark.client.data.model.senders.SignatureDetails;
import com.wildbit.java.postmark.client.data.model.senders.Signatures;
import com.wildbit.java.postmark.client.exception.PostmarkException;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Created by bash on 11/14/17.
*/
public class SendersTest extends BaseTest {

AccountApiClient client = getDefaultAccountApiClient();

@Test
void list() throws PostmarkException, IOException {
Signatures senders = client.getSenderSignatures(Parameters.init().build("count",6).build("offset",0));
assertTrue(senders.getTotalCount() > 0);
assertTrue(senders.getSenderSignatures().size() > 0);

}

@Test
void listById() throws PostmarkException, IOException {
Signatures senders = client.getSenderSignatures(Parameters.init().build("count",6).build("offset",0));
Integer senderId = senders.getSenderSignatures().get(0).getId();
SignatureDetails senderDetails = client.getSenderSignatureDetails(senderId);
assertNotNull(senderDetails.getConfirmed());
}


}
109 changes: 109 additions & 0 deletions src/test/java/integration/TemplateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package integration;

import base.BaseTest;
import com.wildbit.java.postmark.client.ApiClient;
import com.wildbit.java.postmark.client.Parameters;
import com.wildbit.java.postmark.client.data.model.bounces.Bounce;
import com.wildbit.java.postmark.client.data.model.bounces.BounceDump;
import com.wildbit.java.postmark.client.data.model.bounces.Bounces;
import com.wildbit.java.postmark.client.data.model.bounces.DeliveryStats;
import com.wildbit.java.postmark.client.data.model.templates.*;
import com.wildbit.java.postmark.client.exception.PostmarkException;
import org.junit.jupiter.api.Test;



import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Created by bash on 11/14/17.
*/
public class TemplateTest extends BaseTest {

ApiClient client = getDefaultApiClient();

@Test
void validateHtmlBody() throws PostmarkException, IOException {
TemplateToValidate templateToValidate = new TemplateToValidate();
templateToValidate.setSubject("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setHtmlBody("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setTextBody("{{#company}}{{phone}}{{/company}}{{#each person}} {{name}} {{/each}}");

HashMap renderModel = new HashMap<String, Object>();
renderModel.put("userName", "bobby joe");
templateToValidate.setTestRenderModel(renderModel);

TemplateValidation validation = client.validateTemplate(templateToValidate);
assertEquals(validation.getHtmlBody().getContentIsValid(), true);
assertEquals(validation.getHtmlBody().getValidationErrors().size(),0);
assertNotNull(validation.getHtmlBody().getRenderedContent());

}

@Test
void validateTextBody() throws PostmarkException, IOException {
TemplateToValidate templateToValidate = new TemplateToValidate();
templateToValidate.setSubject("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setHtmlBody("{{#company}}{{name}}{{/company}} {{subjectHeadline}}");
templateToValidate.setTextBody("{{#company}}{{phone}}{{/company}}{{#each person}} {{name}} {{/each}}");

HashMap renderModel = new HashMap<String, Object>();
renderModel.put("userName", "bobby joe");
templateToValidate.setTestRenderModel(renderModel);

TemplateValidation validation = client.validateTemplate(templateToValidate);
assertEquals(validation.getTextBody().getContentIsValid(), true);
assertEquals(validation.getTextBody().getValidationErrors().size(),0);
assertNotNull(validation.getTextBody().getRenderedContent());
}

@Test
void createTemplate() throws PostmarkException, IOException {

String templateName = "name";

TemplateContent templateContent = new TemplateContent();
templateContent.setHtmlBody("test html");
templateContent.setTextBody("test text");
templateContent.setName(templateName);
templateContent.setSubject("subject");

BaseTemplate response = client.createTemplate(templateContent);
assertEquals(response.getName(),templateName);

Integer id = response.getTemplateId();
client.deleteTemplate(id);

}

@Test
void deleteTemplate() throws PostmarkException, IOException {
String templateName = "deleteName";

TemplateContent templateContent = new TemplateContent();
templateContent.setHtmlBody("test html");
templateContent.setTextBody("test text");
templateContent.setName(templateName);
templateContent.setSubject("subject");

BaseTemplate response = client.createTemplate(templateContent);
Integer id = response.getTemplateId();
String stringResponse = client.deleteTemplate(id);
assertNotNull(stringResponse);

}

@Test
void list() throws PostmarkException, IOException {
Templates templates = client.getTemplates(Parameters.init().build("offset", 0).build("count", 4));
assertNotNull(templates.getTotalCount());

}

}
34 changes: 33 additions & 1 deletion src/test/java/unit/data/MessageTest.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package unit.data;


import base.BaseTest;
import com.wildbit.java.postmark.client.Parameters;
import com.wildbit.java.postmark.client.data.DataHandler;
import com.wildbit.java.postmark.client.data.model.message.Message;
import com.wildbit.java.postmark.client.exception.InvalidMessageException;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Created by bash on 11/13/17.
*/
public class MessageTest {
public class MessageTest extends BaseTest {

DataHandler dataHandler = new DataHandler();
String fromAddress = "[email protected]";
Expand Down Expand Up @@ -148,4 +155,29 @@ void simpleMessageBcccRecipientsFullName() throws IOException {

}

@Test
void attachmentException() throws IOException {
Message message = new Message();
assertThrows(java.nio.file.NoSuchFileException.class, () -> message.addAttachment("test"));
}

@Test
void addAttachment() throws IOException {
Message message = new Message("[email protected]","[email protected]","Hello world", "Hello world");
message.addAttachment(getDefaultFilePath());

HashMap<String,String> attachment = message.getAttachments().get(0);
assertEquals(attachment.get("ContentType"), "application/pdf");
assertEquals(attachment.get("Name"), new File(getDefaultFilePath()).getName());
assertNotNull(attachment.get("Content"));
}

@Test
void addMultipleAttachments() throws IOException {
Message message = new Message("[email protected]","[email protected]","Hello world", "Hello world");
message.addAttachment(getDefaultFilePath());
message.addAttachment(getDefaultFilePath());
assertEquals(message.getAttachments().size(), 2);
}

}

0 comments on commit 006ede5

Please sign in to comment.