Upload files to 'src/test/java/ca/mgamble/postman/api'

This commit is contained in:
nikkel 2020-03-20 03:22:30 +07:00
parent ce812fee6c
commit aeafc6fe4b
3 changed files with 175 additions and 0 deletions

View File

@ -0,0 +1,33 @@
package ca.mgamble.postman.api.message;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.assertEquals;
class PostmanMessageBuilderTest {
@Test
void builderTest() {
PostmanMessage message = new PostmanMessageBuilder()
.from("no-reply@my-domain.com")
.to("email@gmail.com")
.to("email2@gmail.com")
.cc("cc@gmail.com")
.withSubject("Testing with attachments")
.withPlainBody("Please view this email in a modern email client!")
.withHtmlBody("<html><h1>html body</h1><h2>Please check your attachments!</h2></html>")
.addAttachment(new Attachment("test-csv.csv", "application/octet-stream", "base64String"))
.withTag("tag")
.build();
assertEquals("no-reply@my-domain.com", message.getFrom());
assertEquals(Arrays.asList("email@gmail.com", "email2@gmail.com"), message.getTo());
assertEquals(new ArrayList<>(), message.getBcc());
assertEquals(Collections.singletonList("cc@gmail.com"), message.getCc());
assertEquals("Testing with attachments", message.getSubject());
}
}

View File

@ -0,0 +1,63 @@
package ca.mgamble.postman.api;
import ca.mgamble.postman.api.message.Attachment;
import ca.mgamble.postman.api.message.EmbeddedImage;
import ca.mgamble.postman.api.message.PostmanMessage;
import ca.mgamble.postman.api.message.PostmanMessageBuilder;
import ca.mgamble.postman.api.response.OperationStatus;
import ca.mgamble.postman.api.response.PostmanApiResponse;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
class PostmanRawServiceTest {
public static final String TO_ADRESS = "email@test.com";
public static final String POSTMAN_URL = "https://postman.prmail.vn";
public static final String POSTMAN_API_KEY = "API_KEY";
public static final String FROM_ADRESS = "no-reply@my-domain.com";
private PostmanService postmanService;
@BeforeEach
void setUp() {
postmanService = new PostmanService(POSTMAN_URL, POSTMAN_API_KEY);
}
@Disabled("Only used to test sending email via API Postman, not to be run automatically")
@Test
public void sendMailTest_withEmbeddedImage_withAttachment_shouldSendRawMessage() throws Exception {
// GIVEN
byte[] googleLogo = IOUtils.toByteArray(Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("google.png")));
byte[] csvFile = IOUtils.toByteArray(Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("test-csv.csv")));
PostmanMessage message = new PostmanMessageBuilder()
.from(FROM_ADRESS)
.to(TO_ADRESS)
.withSubject("Postman Java Google")
.withPlainBody("Please view this email in a modern email client!")
.withHtmlBody(getEmailBody())
.addEmbeddedImage(new EmbeddedImage("google", "image/png", googleLogo))
.addAttachment(new Attachment("test-csv.csv", "application/octet-stream", csvFile))
.withTag("tag_name")
.build();
// WHEN
PostmanApiResponse postmanApiResponse = postmanService.sendMessage(message);
// THEN
assertNotNull(postmanApiResponse);
assertEquals(OperationStatus.SUCCESS, postmanApiResponse.getStatus());
}
public String getEmailBody() throws IOException {
File file = new File(this.getClass().getClassLoader().getResource("email.html").getFile());
return new String(Files.readAllBytes(file.toPath()));
}
}

View File

@ -0,0 +1,79 @@
package ca.mgamble.postman.api;
import ca.mgamble.postman.api.message.Attachment;
import ca.mgamble.postman.api.message.PostmanMessage;
import ca.mgamble.postman.api.message.PostmanMessageBuilder;
import ca.mgamble.postman.api.response.OperationStatus;
import ca.mgamble.postman.api.response.PostmanApiResponse;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.Objects;
import static ca.mgamble.postman.api.PostmanRawServiceTest.FROM_ADRESS;
import static ca.mgamble.postman.api.PostmanRawServiceTest.POSTMAN_API_KEY;
import static ca.mgamble.postman.api.PostmanRawServiceTest.POSTMAN_URL;
import static ca.mgamble.postman.api.PostmanRawServiceTest.TO_ADRESS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
class PostmanServiceTest {
private PostmanService postmanService;
@BeforeEach
void setUp() {
postmanService = new PostmanService(POSTMAN_URL, POSTMAN_API_KEY);
}
@Disabled("Only used to test sending email via API Postman, not to be run automatically")
@Test
public void sendMailTest_withAttachments() throws Exception {
// GIVEN
PostmanMessage message = buildPostmanMessageWithAttachments(TO_ADRESS);
// WHEN
PostmanApiResponse postmanApiResponse = postmanService.sendMessage(message);
// THEN
assertNotNull(postmanApiResponse);
assertEquals(OperationStatus.SUCCESS, postmanApiResponse.getStatus());
}
@Test
public void onSendMail_withBadPostmanServiceUrl_shouldThrowException() throws Exception {
// GIVEN
PostmanMessage message = buildPostmanMessageWithAttachments("test@email.com");
// WHEN
Exception exception = assertThrows(Exception.class, () -> postmanService.sendMessage(message));
// THEN
assertEquals("Could not send message, server responded with 404", exception.getMessage());
}
private PostmanMessage buildPostmanMessageWithAttachments(String to) throws IOException {
PostmanMessage message = new PostmanMessageBuilder()
.from(FROM_ADRESS)
.to(to)
.withSubject("Testing with attachments")
.withPlainBody("Please view this email in a modern email client!")
.withHtmlBody("<html><h1>html body</h1><h2>Please check your attachments!</h2></html>")
.withTag("tag_name")
.build();
try (InputStream inputStream = Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("test-csv.csv"))) {
byte[] tblBordByte = IOUtils.toByteArray(inputStream);
message.addAttachment(new Attachment("test-csv.csv", "application/octet-stream", new String(Base64.getEncoder().encode(tblBordByte))));
}
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("test-text.txt")) {
byte[] tblBordByteTxt = IOUtils.toByteArray(Objects.requireNonNull(inputStream));
message.addAttachment(new Attachment("test-text.txt", "text/plain", new String(Base64.getEncoder().encode(tblBordByteTxt))));
}
return message;
}
}