Skip to content

Commit

Permalink
#33: Sonar fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gessnerfl committed Nov 13, 2020
1 parent 67c884b commit 25fd169
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,28 @@ void shouldReturnResponseEntityForAttachment() {

@Test
void shouldThrowExceptionWhenNoAttachmentExistsForTheGivenId() {
assertThrows(AttachmentNotFoundException.class, () -> {
var emailId = 123L;
var attachmentId = 456L;
var email = mock(Email.class);
var attachment = mock(EmailAttachment.class);
var emailId = 123L;
var attachmentId = 456L;
var email = mock(Email.class);
var attachment = mock(EmailAttachment.class);

when(email.getId()).thenReturn(789L);
when(attachment.getEmail()).thenReturn(email);
when(emailAttachmentRepository.findById(attachmentId)).thenReturn(Optional.of(attachment));
when(email.getId()).thenReturn(789L);
when(attachment.getEmail()).thenReturn(email);
when(emailAttachmentRepository.findById(attachmentId)).thenReturn(Optional.of(attachment));

assertThrows(AttachmentNotFoundException.class, () -> {
sut.getEmailAttachmentById(emailId, attachmentId);
});
}

@Test
void shouldThrowExceptionWhenAttachmentExistsForTheGivenIdButTheEmailIdDoesNotMatch() {
assertThrows(AttachmentNotFoundException.class, () -> {
var emailId = 123L;
var attachmentId = 456L;
var emailId = 123L;
var attachmentId = 456L;

when(emailAttachmentRepository.findById(attachmentId)).thenReturn(Optional.empty());
when(emailAttachmentRepository.findById(attachmentId)).thenReturn(Optional.empty());

assertThrows(AttachmentNotFoundException.class, () -> {
sut.getEmailAttachmentById(emailId, attachmentId);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,38 @@ void shouldThrowLoginFailedExceptionWhenPasswordIsNotValid() {

@Test
void shouldThrowNullPointerExceptionWhenAuthenticationIsMissing() {
Assertions.assertThrows(NullPointerException.class, () -> {
var username = "username";
var password = "password";
when(fakeSmtpConfigurationProperties.getAuthentication()).thenReturn(null);
var username = "username";
var password = "password";
when(fakeSmtpConfigurationProperties.getAuthentication()).thenReturn(null);

Assertions.assertThrows(NullPointerException.class, () -> {
sut.login(username, password);
});
}

@Test
void shouldThrowNullPointerExceptionWhenUsernameIsMissingInAuthentication() {
Assertions.assertThrows(NullPointerException.class, () -> {
var username = "username";
var password = "password";
var authentication = mock(FakeSmtpConfigurationProperties.Authentication.class);
when(authentication.getUsername()).thenReturn(null);
when(fakeSmtpConfigurationProperties.getAuthentication()).thenReturn(authentication);
var username = "username";
var password = "password";
var authentication = mock(FakeSmtpConfigurationProperties.Authentication.class);
when(authentication.getUsername()).thenReturn(null);
when(fakeSmtpConfigurationProperties.getAuthentication()).thenReturn(authentication);

Assertions.assertThrows(NullPointerException.class, () -> {
sut.login(username, password);
});
}

@Test
void shouldThrowNullPointerExceptionWhenPasswordIsMissingInAuthentication() {
Assertions.assertThrows(NullPointerException.class, () -> {
var username = "username";
var password = "password";
var authentication = mock(FakeSmtpConfigurationProperties.Authentication.class);
when(authentication.getUsername()).thenReturn(username);
when(authentication.getPassword()).thenReturn(null);
when(fakeSmtpConfigurationProperties.getAuthentication()).thenReturn(authentication);
var username = "username";
var password = "password";
var authentication = mock(FakeSmtpConfigurationProperties.Authentication.class);
when(authentication.getUsername()).thenReturn(username);
when(authentication.getPassword()).thenReturn(null);
when(fakeSmtpConfigurationProperties.getAuthentication()).thenReturn(authentication);

Assertions.assertThrows(NullPointerException.class, () -> {
sut.login(username, password);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.gessnerfl.fakesmtp.TestResourceUtil;
import de.gessnerfl.fakesmtp.model.ContentType;
import de.gessnerfl.fakesmtp.model.Email;
import de.gessnerfl.fakesmtp.model.EmailAttachment;
import de.gessnerfl.fakesmtp.model.EmailContent;
import de.gessnerfl.fakesmtp.util.TimestampProvider;
Expand Down Expand Up @@ -44,6 +45,10 @@ void shouldCreateEmailForEmlFileWithSubjectAndContentTypePlain() throws Exceptio

var result = sut.convert(rawData);

assertPlainTextEmail(now, dataAsString, result);
}

private void assertPlainTextEmail(Date now, String dataAsString, Email result) {
assertEquals(SENDER, result.getFromAddress());
assertEquals(RECEIVER, result.getToAddress());
assertEquals("This is the mail title", result.getSubject());
Expand Down Expand Up @@ -92,16 +97,7 @@ void shouldCreateEmailForEmlFileWithSubjectAndWithoutContentType() throws Except

var result = sut.convert(rawData);

assertEquals(SENDER, result.getFromAddress());
assertEquals(RECEIVER, result.getToAddress());
assertEquals("This is the mail title", result.getSubject());
assertEquals(dataAsString, result.getRawData());
assertThat(result.getContents(), hasSize(1));
assertFalse(result.getHtmlContent().isPresent());
assertTrue(result.getPlainContent().isPresent());
assertEquals("This is the message content", result.getPlainContent().get().getData());
assertEquals(now, result.getReceivedOn());
assertThat(result.getAttachments(), empty());
assertPlainTextEmail(now, dataAsString, result);
}

@Test
Expand Down Expand Up @@ -188,16 +184,7 @@ void shouldCreateMailForMultipartWithoutContentTypeHtml() throws Exception {

var result = sut.convert(rawData);

assertEquals(SENDER, result.getFromAddress());
assertEquals(RECEIVER, result.getToAddress());
assertEquals("This is the mail title", result.getSubject());
assertEquals(dataAsString, result.getRawData());
assertThat(result.getContents(), hasSize(1));
assertFalse(result.getHtmlContent().isPresent());
assertTrue(result.getPlainContent().isPresent());
assertEquals("This is the message content", result.getPlainContent().get().getData());
assertEquals(now, result.getReceivedOn());
assertThat(result.getAttachments(), empty());
assertPlainTextEmail(now, dataAsString, result);
}

@Test
Expand Down

0 comments on commit 25fd169

Please sign in to comment.