Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public Builder to(String... recipients) {
* @return This builder instance for method chaining.
*/
public Builder to(List<String> recipients) {
this.to = recipients;
this.to = recipients == null ? null : new ArrayList<>(recipients);
return this;
}

Expand Down Expand Up @@ -436,7 +436,7 @@ public Builder html(String html) {
* @return This builder instance for method chaining.
*/
public Builder headers(Map<String, String> headers) {
this.headers = headers;
this.headers = headers == null ? null : new HashMap<>(headers);
return this;
}

Expand Down Expand Up @@ -478,7 +478,7 @@ public Builder attachments(Attachment... attachments) {
* @return This builder instance for method chaining.
*/
public Builder attachments(List<Attachment> attachments) {
this.attachments = attachments;
this.attachments = attachments == null ? null : new ArrayList<>(attachments);
return this;
}

Expand Down Expand Up @@ -519,7 +519,7 @@ public Builder tags(Tag... tags) {
* @return This builder instance for method chaining.
*/
public Builder tags(List<Tag> tags) {
this.tags = tags;
this.tags = tags == null ? null : new ArrayList<>(tags);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.resend.services.emails;

import com.resend.services.emails.model.Attachment;
import com.resend.services.emails.model.CreateEmailOptions;
import com.resend.services.emails.model.Tag;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

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

public class CreateEmailOptionsTest {

private static CreateEmailOptions.Builder base() {
return CreateEmailOptions.builder().from("sender@example.com").subject("Subject");
}

@Test
public void toListAcceptsImmutableListAndAllowsAddTo() {
CreateEmailOptions options = base()
.to(Collections.singletonList("a@example.com"))
.addTo("b@example.com")
.build();

assertEquals(Arrays.asList("a@example.com", "b@example.com"), options.getTo());
}

@Test
public void toListDoesNotWriteThroughToCallerList() {
List<String> recipients = new ArrayList<>(Collections.singletonList("a@example.com"));

CreateEmailOptions options = base().to(recipients).addTo("b@example.com").build();
recipients.add("c@example.com");

assertEquals(Arrays.asList("a@example.com", "c@example.com"), recipients);
assertEquals(Arrays.asList("a@example.com", "b@example.com"), options.getTo());
}

@Test
public void tagsListAcceptsImmutableListAndAllowsAddTag() {
Tag first = Tag.builder().name("a").value("1").build();
Tag second = Tag.builder().name("b").value("2").build();

CreateEmailOptions options = base()
.tags(Collections.singletonList(first))
.addTag(second)
.build();

assertEquals(Arrays.asList(first, second), options.getTags());
}

@Test
public void attachmentsListAcceptsImmutableListAndAllowsAddAttachment() {
Attachment first = Attachment.builder().fileName("a.txt").content("YQ==").build();
Attachment second = Attachment.builder().fileName("b.txt").content("Yg==").build();

CreateEmailOptions options = base()
.attachments(Collections.singletonList(first))
.addAttachment(second)
.build();

assertEquals(Arrays.asList(first, second), options.getAttachments());
}

@Test
public void headersMapAcceptsImmutableMapAndAllowsAddHeader() {
CreateEmailOptions options = base()
.headers(Collections.singletonMap("X-One", "1"))
.addHeader("X-Two", "2")
.build();

Map<String, String> headers = options.getHeaders();
assertEquals("1", headers.get("X-One"));
assertEquals("2", headers.get("X-Two"));
}
}
Loading