-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat(VIST-CPC-1361) See Received emails #1018
Open
FelixAllard
wants to merge
6
commits into
main
Choose a base branch
from
feat/VIST-CPC-1361_see_received_emails
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cecda01
First commit
FelixAllard e834395
Added Tests!
FelixAllard 2c413cd
Finished the testing!
FelixAllard 6da8050
Merge branch 'main' into feat/VIST-CPC-1361_see_received_emails
Justin222993 1a795b8
Merge branch 'main' into feat/VIST-CPC-1361_see_received_emails
Justin222993 a3b9588
Merge branch 'main' into feat/VIST-CPC-1361_see_received_emails
FelixAllard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...way/src/main/java/com/petclinic/bffapigateway/dtos/Emailing/ReceivedEmailResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.petclinic.bffapigateway.dtos.Emailing; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Date; | ||
|
||
@Builder | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class ReceivedEmailResponseDTO { | ||
public String from ; // The email address of the sender | ||
public String subject ; // The subject line of the email | ||
public Date dateReceived ; // The date and time the email was sent | ||
public String plainTextBody ; // The main text content of the email | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
package com.petclinic.bffapigateway.domainclientlayer; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.petclinic.bffapigateway.domainclientlayer.EmailingServiceClient; | ||
import com.petclinic.bffapigateway.dtos.Emailing.DirectEmailModelRequestDTO; | ||
import com.petclinic.bffapigateway.dtos.Emailing.EmailModelResponseDTO; | ||
import com.petclinic.bffapigateway.dtos.Emailing.NotificationEmailModelRequestDTO; | ||
import com.petclinic.bffapigateway.dtos.Emailing.RawEmailModelRequestDTO; | ||
|
||
import com.petclinic.bffapigateway.dtos.Emailing.*; | ||
import okhttp3.mockwebserver.MockResponse; | ||
import okhttp3.mockwebserver.MockWebServer; | ||
import org.junit.jupiter.api.AfterAll; | ||
|
@@ -18,11 +15,15 @@ | |
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.test.StepVerifier; | ||
import reactor.test.StepVerifier; | ||
|
||
|
||
import java.io.IOException; | ||
|
||
import java.time.Instant; | ||
|
||
import java.time.ZoneOffset; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
public class EmailingServiceClientIntegrationTest { | ||
|
||
|
@@ -147,4 +148,25 @@ void sendRawEmail() throws Exception { | |
.expectNext(HttpStatus.OK) | ||
.verifyComplete(); | ||
} | ||
@Test | ||
void getAllReceivedEmails() throws Exception { | ||
// Create a sample ReceivedEmailResponseDTO object | ||
ReceivedEmailResponseDTO receivedEmailResponseDTO = new ReceivedEmailResponseDTO( | ||
"[email protected]", "Subject", new Date(), "a body" | ||
); | ||
|
||
// Enqueue a mock response from the server | ||
mockWebServer.enqueue(new MockResponse() | ||
.setHeader(org.springframework.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||
.setBody(objectMapper.writeValueAsString(List.of(receivedEmailResponseDTO))) // Wrap in a list for the Flux response | ||
); | ||
|
||
// Call the method to test | ||
Flux<ReceivedEmailResponseDTO> emailFlux = emailingServiceClient.getAllReceivedEmails(); | ||
|
||
// Verify the response using StepVerifier | ||
StepVerifier.create(emailFlux) | ||
.expectNext(receivedEmailResponseDTO) // Expect the received email response DTO | ||
.verifyComplete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,28 @@ public void registerGetAllEmailsEndpoint() { | |
.withBody(json(emailResponse)) | ||
); | ||
} | ||
public void registerGetAllReceivedEmailsEndpoint() { | ||
String receivedEmailResponse = "[" + | ||
"{" + | ||
"\"from\": \"\\\"Xilef992\\\" <[email protected]>\"," + | ||
"\"subject\": \"yeah ok\"," + | ||
"\"dateReceived\": \"2024-10-20T21:52:40.000+00:00\"," + | ||
"\"plainTextBody\": \"You are done!\\n\"" + | ||
"}" + | ||
"]"; | ||
|
||
mockServerClient_EmailingService | ||
.when( | ||
request() | ||
.withMethod("GET") | ||
.withPath("/received/all") | ||
) | ||
.respond( | ||
response() | ||
.withStatusCode(200) | ||
.withBody(json(receivedEmailResponse)) | ||
); | ||
} | ||
|
||
// Mock for addHtmlTemplate endpoint | ||
public void registerAddHtmlTemplateEndpoint() { | ||
|
@@ -86,6 +108,7 @@ public void registerSendEmailNotificationEndpoint() { | |
); | ||
} | ||
|
||
|
||
// Stop the mock server | ||
public void stopMockServer() { | ||
if (clientAndServer != null) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,14 @@ | |
using emailing_service.Models; | ||
using emailing_service.Models.Database; | ||
using emailing_service.Models.EmailType; | ||
using emailing_service.Models.SMTP.Model; | ||
using emailing_service.Utils; | ||
using emailing_service.Utils.Exception; | ||
using MailKit; | ||
using MailKit.Net.Imap; | ||
using MailKit.Search; | ||
using MimeKit; | ||
using Moq; | ||
|
||
namespace emailing_service_test.BuisnessLayer; | ||
|
||
|
@@ -15,6 +21,8 @@ namespace emailing_service_test.BuisnessLayer; | |
[TestFixture] | ||
public class EmailServiceImplTest | ||
{ | ||
private Mock<ImapClient> _imapClientMock; | ||
private Mock<IMailFolder> _inboxMock; | ||
private IEmailService _controller; | ||
private readonly string _pathOfDefaultHtml; | ||
public EmailServiceImplTest() | ||
|
@@ -26,6 +34,20 @@ public EmailServiceImplTest() | |
[OneTimeSetUp] | ||
public Task SetUp() | ||
{ | ||
var mockEmail1 = new MimeMessage(); | ||
mockEmail1.From.Add(new MailboxAddress("Sender One", "[email protected]")); | ||
mockEmail1.Subject = "Test Email 1"; | ||
mockEmail1.Date = DateTimeOffset.Now; | ||
mockEmail1.Body = new TextPart("plain") { Text = "Hello from sender 1" }; | ||
|
||
var mockEmail2 = new MimeMessage(); | ||
mockEmail2.From.Add(new MailboxAddress("Sender Two", "[email protected]")); | ||
mockEmail2.Subject = "Test Email 2"; | ||
mockEmail2.Date = DateTimeOffset.Now; | ||
mockEmail2.Body = new TextPart("plain") { Text = "Hello from sender 2" }; | ||
_imapClientMock = new Mock<ImapClient>(); | ||
_inboxMock = new Mock<IMailFolder>(); // Mock IMailFolder for inbox | ||
_imapClientMock.Setup(client => client.Inbox).Returns(_inboxMock.Object); // Return mocked inbox | ||
EmailUtils.emailConnectionString = new ConnectionEmailServer( | ||
"Mock", | ||
000, | ||
|
@@ -36,6 +58,7 @@ public Task SetUp() | |
); | ||
_controller = new EmailServiceImpl(); | ||
_controller.SetDatabaseHelper(new TestDbContext()); | ||
_controller.SetImapServer(_imapClientMock.Object); | ||
return Task.CompletedTask; | ||
} | ||
/*[Test] | ||
|
@@ -685,6 +708,28 @@ public void SendEmailNotification_AlreadyPassedDate_AlreadyPassedDate(Notificati | |
Does.Contain($"The Date for the notification was already passed!")); | ||
} | ||
|
||
[Test] | ||
public async Task GetAllEmailsReceivedAsync_ShouldThrowAuthenticationException_WhenInvalidCredentials() | ||
{ | ||
// Act & Assert | ||
var ex = Assert.ThrowsAsync<System.NullReferenceException>(async () => | ||
{ | ||
await foreach (var email in _controller.GetAllEmailsReceivedAsync()) | ||
{ | ||
// We shouldn't get to this point | ||
} | ||
}); | ||
|
||
Assert.That(ex.Message, Is.EqualTo("Object reference not set to an instance of an object.")); // Adjust according to your actual exception message | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
[OneTimeTearDown] | ||
public void TearDown() | ||
|
58 changes: 58 additions & 0 deletions
58
emailing-service/emailing-service-test/Models/SMTP/Model/EmailReceivedTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using emailing_service.Models.SMTP.Model; | ||
|
||
namespace emailing_service_test.Models.SMTP.Model; | ||
|
||
using NUnit.Framework; | ||
using System; | ||
|
||
[TestFixture] | ||
public class EmailReceivedTests | ||
{ | ||
[Test] | ||
public void EmailReceived_Constructor_ShouldInitializeProperties() | ||
{ | ||
// Arrange | ||
string from = "[email protected]"; | ||
string subject = "Test Subject"; | ||
DateTime dateReceived = DateTime.Now; | ||
string plainTextBody = "This is a test email body."; | ||
|
||
// Act | ||
var emailReceived = new EmailReceived(from, subject, dateReceived, plainTextBody); | ||
|
||
// Assert | ||
Assert.AreEqual(from, emailReceived.From); | ||
Assert.AreEqual(subject, emailReceived.Subject); | ||
Assert.AreEqual(dateReceived, emailReceived.DateReceived); | ||
Assert.AreEqual(plainTextBody, emailReceived.PlainTextBody); | ||
} | ||
|
||
[Test] | ||
public void EmailReceived_SetAndGetProperties_ShouldWorkCorrectly() | ||
{ | ||
// Arrange | ||
var emailReceived = new EmailReceived("[email protected]", "Initial Subject", DateTime.Now, "Initial Body"); | ||
|
||
// Act | ||
emailReceived.From = "[email protected]"; | ||
emailReceived.Subject = "Updated Subject"; | ||
emailReceived.DateReceived = DateTime.Now.AddDays(-1); | ||
emailReceived.PlainTextBody = "Updated Body"; | ||
|
||
// Assert | ||
Assert.AreEqual("[email protected]", emailReceived.From); | ||
Assert.AreEqual("Updated Subject", emailReceived.Subject); | ||
Assert.IsTrue(emailReceived.DateReceived < DateTime.Now); // Assuming you want to check if the date is before now | ||
Assert.AreEqual("Updated Body", emailReceived.PlainTextBody); | ||
} | ||
|
||
[Test] | ||
public void EmailReceived_EmptyConstructor_ShouldNotThrowException() | ||
{ | ||
// Arrange & Act | ||
var emailReceived = new EmailReceived(string.Empty, string.Empty, DateTime.MinValue, string.Empty); | ||
|
||
// Assert | ||
Assert.IsNotNull(emailReceived); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this port still open and accessible when upping the system? If so, it should be closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you are right! I will fix that!