Skip to content

Commit

Permalink
✅ (#236): Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Klaus committed Jan 16, 2025
1 parent 5a9670b commit 1f7e0bd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependency-track-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- "app.hopps.az-document-ai"
- "app.hopps.fin"
- "app.hopps.zugferd"
- "app-hopps.mailservice"
- "app.hopps.mailservice"

steps:
# Checkout the code
Expand Down
6 changes: 6 additions & 0 deletions backend/app.hopps.mailservice/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@
<version>4.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.18</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package app.hopps.mailservice;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

public record Mail(String[] mailReceivers, MailTemplates templateId, Map<String, String> variables) {

private static final ObjectMapper objectMapper = new ObjectMapper();

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
if (o == null || getClass() != o.getClass())
return false;
Mail mail = (Mail) o;
return Objects.deepEquals(mailReceivers, mail.mailReceivers) && templateId == mail.templateId && Objects.equals(variables, mail.variables);
return Objects.deepEquals(mailReceivers, mail.mailReceivers) && templateId == mail.templateId
&& Objects.equals(variables, mail.variables);
}

@Override
Expand All @@ -19,10 +27,14 @@ public int hashCode() {

@Override
public String toString() {
return "Mail{" +
"mailReceivers=" + Arrays.toString(mailReceivers) +
", templateId=" + templateId +
", variables=" + variables +
'}';
try {
return objectMapper.writeValueAsString(this);
} catch (JsonProcessingException ignored) {
return "{" +
"mailReceivers: [" + String.join(",", mailReceivers) + "]," +
"templateId: " + templateId + ',' +
"variables: " + variables +
'}';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Uni<Void> handleRequest(Mail mail) {
.invoke(throwable -> LOG.info("Mail could not be sent", throwable));
}

private Function<Map<String, String>, MailTemplate.MailTemplateInstance> getTemplateByType(MailTemplates mailTemplates) {
private Function<Map<String, String>, MailTemplate.MailTemplateInstance> getTemplateByType(
MailTemplates mailTemplates) {
return switch (mailTemplates) {
case TEMP -> Templates::temp;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -37,7 +38,7 @@ void shouldSendMail() {
InMemorySource<Mail> mailSender = connector.source("mail");
mailSender.runOnVertxContext(true);

Mail mail = new Mail(new String[]{"[email protected]"}, MailTemplates.TEMP, Map.of("name", "Peter"));
Mail mail = new Mail(new String[] { "[email protected]" }, MailTemplates.TEMP, Map.of("name", "Peter"));

// when
mailSender.send(mail);
Expand All @@ -48,4 +49,24 @@ void shouldSendMail() {
assertEquals(1, mailsSentTo.size());
assertEquals(1, mockMailbox.getTotalMessagesSent());
}
}

@Test
void shouldSendMultipleMailsParallel() {
// given
InMemorySource<Mail> mailSender = connector.source("mail");
mailSender.runOnVertxContext(true);

Mail mail = new Mail(new String[] { "[email protected]" }, MailTemplates.TEMP, Map.of("name", "Peter"));

// when
for (int i = 0; i < 500; i++) {
mailSender.send(mail);
}
await().atMost(Duration.ofSeconds(1)).until(() -> mockMailbox.getTotalMessagesSent() == 500);

// then
List<io.quarkus.mailer.Mail> mailsSentTo = mockMailbox.getMailsSentTo("[email protected]");
assertEquals(500, mailsSentTo.size());
assertEquals(500, mockMailbox.getTotalMessagesSent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package app.hopps.mailservice;

import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

import java.util.Map;

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

class MailTest {
@Test
void testEquals() {
EqualsVerifier.forClass(Mail.class).verify();
}

@Test
void testToString() {
Mail mail = new Mail(new String[] { "[email protected]", "[email protected]" }, MailTemplates.TEMP, Map.of());

String expected = "{\"mailReceivers\":[\"[email protected]\",\"[email protected]\"],\"templateId\":\"TEMP\",\"variables\":{}}";

String actual = mail.toString();

assertEquals(expected, actual);
}
}

0 comments on commit 1f7e0bd

Please sign in to comment.