Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/mailersend/sdk/emails/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void AddImageFromFile(String contentId, String path) throws IOException {
this.id = contentId;
this.content = Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(file));
this.filename = file.getName();
setDisposition(contentId != null && contentId.trim().length() > 1);
setDisposition(contentId != null && contentId.trim().length() > 0);
}

/**
Expand All @@ -86,7 +86,7 @@ public void setImage(String contentId, String content, String filename) {
this.id = contentId;
this.content = content;
this.filename = filename;
setDisposition(contentId != null && contentId.trim().length() > 1);
setDisposition(contentId != null && contentId.trim().length() > 0);
}

/**
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/mailersend/sdk/tests/AttachmentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mailersend.sdk.tests;

import com.google.gson.Gson;
import com.mailersend.sdk.emails.Attachment;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

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

public class AttachmentTest {
@TempDir
Path directory;

@ParameterizedTest
@CsvSource({"x, inline", "xx, inline", "' x ', inline", "NULL, attachment", "'', attachment", "' ', attachment"})
void setImageUsesNonEmptyContentId(String contentId, String disposition) {
Attachment attachment = new Attachment();
attachment.setImage(contentId.equals("NULL") ? null : contentId, "AQID", "image.png");
assertImage(attachment, contentId.equals("NULL") ? null : contentId, disposition);
}

@ParameterizedTest
@CsvSource({"x, inline", "xx, inline", "' x ', inline", "NULL, attachment", "'', attachment", "' ', attachment"})
void addImageFromFileUsesNonEmptyContentId(String contentId, String disposition) throws IOException {
Path image = directory.resolve("image.png");
Files.write(image, new byte[]{1, 2, 3});
Attachment attachment = new Attachment();
attachment.AddImageFromFile(contentId.equals("NULL") ? null : contentId, image.toString());
assertImage(attachment, contentId.equals("NULL") ? null : contentId, disposition);
}

private void assertImage(Attachment attachment, String contentId, String disposition) {
assertEquals(contentId, attachment.id);
assertEquals("AQID", attachment.content);
assertEquals("image.png", attachment.filename);
assertEquals(disposition, new Gson().toJsonTree(attachment).getAsJsonObject().get("disposition").getAsString());
}
}