From 504934f4c4400730d06a452d20641d3acd97d9ed Mon Sep 17 00:00:00 2001 From: lucieyang1 Date: Fri, 1 Dec 2023 15:00:49 -0500 Subject: [PATCH 1/6] get session by id instead of code! --- .../data_access/SessionRepository.java | 1 + .../connect/use_case/EmailController.java | 22 +++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/callhub/connect/data_access/SessionRepository.java b/src/main/java/callhub/connect/data_access/SessionRepository.java index d650844..862f598 100644 --- a/src/main/java/callhub/connect/data_access/SessionRepository.java +++ b/src/main/java/callhub/connect/data_access/SessionRepository.java @@ -8,4 +8,5 @@ public interface SessionRepository extends MongoRepository { boolean existsByCodeAndActive(String code, boolean active); Session getSessionsByActiveAndCode(boolean active, String code); + Session getSessionById(String id); } diff --git a/src/main/java/callhub/connect/use_case/EmailController.java b/src/main/java/callhub/connect/use_case/EmailController.java index c567a02..d82671c 100644 --- a/src/main/java/callhub/connect/use_case/EmailController.java +++ b/src/main/java/callhub/connect/use_case/EmailController.java @@ -26,20 +26,20 @@ public EmailController(SessionRepository sessionRepository) { /** * Retrieves the transcript of messages for a given session code. * - * @param code The session code to identify the session. + * @param id The session code to identify the session. * @return A ResponseEntity containing the transcript of messages as a String. * If the session is not found, an error occurs. */ - @GetMapping("/transcript/{code}") - public ResponseEntity getTranscript(@PathVariable String code) { + @GetMapping("/transcript/{id}") + public ResponseEntity getTranscript(@PathVariable String id) { HttpHeaders headers = new HttpHeaders(); - boolean sessionExists = sessionRepository.existsById(code); + boolean sessionExists = sessionRepository.existsById(id); if (!sessionExists) { - new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); + return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); } try { - Session session = sessionRepository.getSessionsByActiveAndCode(true, code); + Session session = sessionRepository.getSessionById(id); ArrayList messagesList = session.getMessages(); StringBuilder transcript = new StringBuilder(); @@ -56,20 +56,20 @@ public ResponseEntity getTranscript(@PathVariable String code) { * Retrieves the date messages were sent for a given session code, assuming all messages * were sent on the same day as the first message. * - * @param code The session code to identify the session. + * @param id The session code to identify the session. * @return A ResponseEntity containing the date of messages as a String. * If the session is not found, an error occurs. */ - @GetMapping("/date/{code}") - public ResponseEntity getDate(@PathVariable String code) { + @GetMapping("/date/{id}") + public ResponseEntity getDate(@PathVariable String id) { HttpHeaders headers = new HttpHeaders(); - boolean sessionExists = sessionRepository.existsById(code); + boolean sessionExists = sessionRepository.existsById(id); if (!sessionExists) { new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); } try { - Session session = sessionRepository.getSessionsByActiveAndCode(true, code); + Session session = sessionRepository.getSessionById(id); ArrayList messagesList = session.getMessages(); Message firstMessage = messagesList.get(0); From 61fa3189895a843a7b54f45568eba777270cc707 Mon Sep 17 00:00:00 2001 From: lucieyang1 Date: Fri, 1 Dec 2023 21:00:49 -0500 Subject: [PATCH 2/6] started to do clean architecture --- .../data_access/EmailDataAccessObject.java | 51 +++++++++++++++++++ .../email/EmailController.java | 30 +++++++++++ .../email/EmailPresenter.java | 12 +++++ ....java => EmailController_fromthepast.java} | 20 +++----- .../email/EmailDataAccessInterface.java | 10 ++++ .../use_case/email/EmailInputBoundary.java | 9 ++++ .../use_case/email/EmailInputData.java | 11 ++++ .../use_case/email/EmailInteractor.java | 38 ++++++++++++++ .../use_case/email/EmailOutputBoundary.java | 5 ++ .../use_case/email/EmailOutputData.java | 11 ++++ 10 files changed, 185 insertions(+), 12 deletions(-) create mode 100644 src/main/java/callhub/connect/data_access/EmailDataAccessObject.java create mode 100644 src/main/java/callhub/connect/interface_adapter/email/EmailController.java create mode 100644 src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java rename src/main/java/callhub/connect/use_case/{EmailController.java => EmailController_fromthepast.java} (79%) create mode 100644 src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java create mode 100644 src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java create mode 100644 src/main/java/callhub/connect/use_case/email/EmailInputData.java create mode 100644 src/main/java/callhub/connect/use_case/email/EmailInteractor.java create mode 100644 src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java create mode 100644 src/main/java/callhub/connect/use_case/email/EmailOutputData.java diff --git a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java new file mode 100644 index 0000000..32b7a63 --- /dev/null +++ b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java @@ -0,0 +1,51 @@ +package callhub.connect.data_access; + +import callhub.connect.entities.Message; +import callhub.connect.entities.Session; +import callhub.connect.use_case.email.EmailDataAccessInterface; + +import java.util.ArrayList; + +public class EmailDataAccessObject implements EmailDataAccessInterface { + private final SessionRepository sessionRepository; + + public EmailDataAccessObject(SessionRepository sessionRepository) { + this.sessionRepository = sessionRepository; + } + + @Override + public String getTranscript(String id) { + boolean sessionExists = sessionRepository.existsById(id); + if (!sessionExists) { + return "This session is inactive or does not exist."; + } + + Session session = sessionRepository.getSessionById(id); + ArrayList messagesList = session.getMessages(); + + StringBuilder transcript = new StringBuilder(); + for (Message message : messagesList) { + transcript.append(message.formattedMessage()).append("\n"); + } + return transcript.toString(); + + } + + @Override + public String getDate(String id) { + boolean sessionExists = sessionRepository.existsById(id); + if (!sessionExists) { + return "This session is inactive or does not exist."; + } + + try { + Session session = sessionRepository.getSessionById(id); + ArrayList messagesList = session.getMessages(); + + Message firstMessage = messagesList.get(0); + return firstMessage.getDateString(); + } catch (Exception e) { + return e.getMessage(); + } + } +} diff --git a/src/main/java/callhub/connect/interface_adapter/email/EmailController.java b/src/main/java/callhub/connect/interface_adapter/email/EmailController.java new file mode 100644 index 0000000..a24733d --- /dev/null +++ b/src/main/java/callhub/connect/interface_adapter/email/EmailController.java @@ -0,0 +1,30 @@ +package callhub.connect.interface_adapter.email; + +import callhub.connect.use_case.email.EmailInputBoundary; +import callhub.connect.use_case.email.EmailInputData; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/email") +public class EmailController { + + @Autowired + private EmailInputBoundary emailInteractor; + + @GetMapping("/transcript/{id}") + public ResponseEntity getTranscript(@PathVariable String id) { + EmailInputData inputData = new EmailInputData(id); + return emailInteractor.getTranscript(inputData); + } + + @GetMapping() + public ResponseEntity getCode(@PathVariable String id) { + EmailInputData inputData = new EmailInputData(id); + return emailInteractor.getDate(inputData); + } +} diff --git a/src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java b/src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java new file mode 100644 index 0000000..f97f788 --- /dev/null +++ b/src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java @@ -0,0 +1,12 @@ +package callhub.connect.interface_adapter.email; + +import callhub.connect.use_case.email.EmailOutputBoundary; +import callhub.connect.use_case.email.EmailOutputData; + +public class EmailPresenter implements EmailOutputBoundary { + + @Override + public String getResponse(EmailOutputData outputData) { + return outputData.getResponseBody(); + } +} diff --git a/src/main/java/callhub/connect/use_case/EmailController.java b/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java similarity index 79% rename from src/main/java/callhub/connect/use_case/EmailController.java rename to src/main/java/callhub/connect/use_case/EmailController_fromthepast.java index d82671c..ef7c198 100644 --- a/src/main/java/callhub/connect/use_case/EmailController.java +++ b/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java @@ -15,11 +15,11 @@ @RestController @RequestMapping("/email") -public class EmailController { +public class EmailController_fromthepast { public SessionRepository sessionRepository; - public EmailController(SessionRepository sessionRepository) { + public EmailController_fromthepast(SessionRepository sessionRepository) { this.sessionRepository = sessionRepository; } @@ -38,18 +38,14 @@ public ResponseEntity getTranscript(@PathVariable String id) { return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); } - try { - Session session = sessionRepository.getSessionById(id); - ArrayList messagesList = session.getMessages(); + Session session = sessionRepository.getSessionById(id); + ArrayList messagesList = session.getMessages(); - StringBuilder transcript = new StringBuilder(); - for (Message message : messagesList) { - transcript.append(message.formattedMessage()).append("\n"); - } - return new ResponseEntity<>(transcript.toString(), headers, HttpStatus.OK); - } catch (Exception e) { - return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST); + StringBuilder transcript = new StringBuilder(); + for (Message message : messagesList) { + transcript.append(message.formattedMessage()).append("\n"); } + return new ResponseEntity<>(transcript.toString(), headers, HttpStatus.OK); } /** diff --git a/src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java b/src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java new file mode 100644 index 0000000..47eb6f0 --- /dev/null +++ b/src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java @@ -0,0 +1,10 @@ +package callhub.connect.use_case.email; + +import java.util.HashMap; + +public interface EmailDataAccessInterface { + + String getTranscript(String id); + + String getDate(String id); +} diff --git a/src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java b/src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java new file mode 100644 index 0000000..d97cd74 --- /dev/null +++ b/src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java @@ -0,0 +1,9 @@ +package callhub.connect.use_case.email; + +import org.springframework.http.ResponseEntity; + +public interface EmailInputBoundary { + ResponseEntity getTranscript(EmailInputData inputData); + + ResponseEntity getDate(EmailInputData inputData); +} diff --git a/src/main/java/callhub/connect/use_case/email/EmailInputData.java b/src/main/java/callhub/connect/use_case/email/EmailInputData.java new file mode 100644 index 0000000..27b6c3d --- /dev/null +++ b/src/main/java/callhub/connect/use_case/email/EmailInputData.java @@ -0,0 +1,11 @@ +package callhub.connect.use_case.email; + +public class EmailInputData { + private final String id; + public EmailInputData(String id) { + this.id = id; + } + public String getId() { + return this.id; + } +} diff --git a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java new file mode 100644 index 0000000..51c41e5 --- /dev/null +++ b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java @@ -0,0 +1,38 @@ +package callhub.connect.use_case.email; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class EmailInteractor implements EmailInputBoundary { + + @Autowired + final EmailDataAccessInterface emailDataAccessObject; + + @Autowired + final EmailOutputBoundary emailPresenter; + + public EmailInteractor(EmailDataAccessInterface emailDataAccessObject, EmailOutputBoundary emailPresenter) { + this.emailDataAccessObject = emailDataAccessObject; + this.emailPresenter = emailPresenter; + } + + @Override + public ResponseEntity getTranscript(EmailInputData inputData) { + HttpHeaders headers = new HttpHeaders(); + String transcript = emailDataAccessObject.getTranscript(inputData.getId()); + EmailOutputData outputData = new EmailOutputData(transcript); + return new ResponseEntity<>(transcript, headers, HttpStatus.OK); + } + + @Override + public ResponseEntity getDate(EmailInputData inputData) { + HttpHeaders headers = new HttpHeaders(); + String date = emailDataAccessObject.getDate(inputData.getId()); + EmailOutputData outputData = new EmailOutputData(date); + return new ResponseEntity<>(date, headers, HttpStatus.OK); + } +} diff --git a/src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java b/src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java new file mode 100644 index 0000000..dedeb22 --- /dev/null +++ b/src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java @@ -0,0 +1,5 @@ +package callhub.connect.use_case.email; + +public interface EmailOutputBoundary { + String getResponse(EmailOutputData outputData); +} diff --git a/src/main/java/callhub/connect/use_case/email/EmailOutputData.java b/src/main/java/callhub/connect/use_case/email/EmailOutputData.java new file mode 100644 index 0000000..f0be694 --- /dev/null +++ b/src/main/java/callhub/connect/use_case/email/EmailOutputData.java @@ -0,0 +1,11 @@ +package callhub.connect.use_case.email; + +public class EmailOutputData { + private final String responseBody; + public EmailOutputData(String responseBody) { + this.responseBody = responseBody; + } + public String getResponseBody() { + return this.responseBody; + } +} From d34a753799dd0995e66b6119f7c2ee42d4e35e47 Mon Sep 17 00:00:00 2001 From: zjayee Date: Fri, 1 Dec 2023 21:52:26 -0500 Subject: [PATCH 3/6] Modified to fit clean architecture --- .../data_access/EmailDataAccessObject.java | 2 + .../email/EmailController.java | 2 +- .../email/EmailPresenter.java | 2 + .../use_case/EmailController_fromthepast.java | 154 +++++++++--------- .../email/EmailDataAccessInterface.java | 4 + .../use_case/email/EmailInputBoundary.java | 2 + .../use_case/email/EmailInteractor.java | 2 - .../use_case/email/EmailOutputBoundary.java | 3 + 8 files changed, 91 insertions(+), 80 deletions(-) diff --git a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java index 32b7a63..9e32914 100644 --- a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java +++ b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java @@ -3,9 +3,11 @@ import callhub.connect.entities.Message; import callhub.connect.entities.Session; import callhub.connect.use_case.email.EmailDataAccessInterface; +import org.springframework.stereotype.Service; import java.util.ArrayList; +@Service public class EmailDataAccessObject implements EmailDataAccessInterface { private final SessionRepository sessionRepository; diff --git a/src/main/java/callhub/connect/interface_adapter/email/EmailController.java b/src/main/java/callhub/connect/interface_adapter/email/EmailController.java index a24733d..90f88e4 100644 --- a/src/main/java/callhub/connect/interface_adapter/email/EmailController.java +++ b/src/main/java/callhub/connect/interface_adapter/email/EmailController.java @@ -22,7 +22,7 @@ public ResponseEntity getTranscript(@PathVariable String id) { return emailInteractor.getTranscript(inputData); } - @GetMapping() + @GetMapping("/date/{id}") public ResponseEntity getCode(@PathVariable String id) { EmailInputData inputData = new EmailInputData(id); return emailInteractor.getDate(inputData); diff --git a/src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java b/src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java index f97f788..18b4601 100644 --- a/src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java +++ b/src/main/java/callhub/connect/interface_adapter/email/EmailPresenter.java @@ -2,7 +2,9 @@ import callhub.connect.use_case.email.EmailOutputBoundary; import callhub.connect.use_case.email.EmailOutputData; +import org.springframework.stereotype.Service; +@Service public class EmailPresenter implements EmailOutputBoundary { @Override diff --git a/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java b/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java index ef7c198..6c62bad 100644 --- a/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java +++ b/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java @@ -1,77 +1,77 @@ -package callhub.connect.use_case; - -import callhub.connect.data_access.SessionRepository; -import callhub.connect.entities.Message; -import callhub.connect.entities.Session; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.ArrayList; - -@RestController -@RequestMapping("/email") -public class EmailController_fromthepast { - - public SessionRepository sessionRepository; - - public EmailController_fromthepast(SessionRepository sessionRepository) { - this.sessionRepository = sessionRepository; - } - - /** - * Retrieves the transcript of messages for a given session code. - * - * @param id The session code to identify the session. - * @return A ResponseEntity containing the transcript of messages as a String. - * If the session is not found, an error occurs. - */ - @GetMapping("/transcript/{id}") - public ResponseEntity getTranscript(@PathVariable String id) { - HttpHeaders headers = new HttpHeaders(); - boolean sessionExists = sessionRepository.existsById(id); - if (!sessionExists) { - return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); - } - - Session session = sessionRepository.getSessionById(id); - ArrayList messagesList = session.getMessages(); - - StringBuilder transcript = new StringBuilder(); - for (Message message : messagesList) { - transcript.append(message.formattedMessage()).append("\n"); - } - return new ResponseEntity<>(transcript.toString(), headers, HttpStatus.OK); - } - - /** - * Retrieves the date messages were sent for a given session code, assuming all messages - * were sent on the same day as the first message. - * - * @param id The session code to identify the session. - * @return A ResponseEntity containing the date of messages as a String. - * If the session is not found, an error occurs. - */ - @GetMapping("/date/{id}") - public ResponseEntity getDate(@PathVariable String id) { - HttpHeaders headers = new HttpHeaders(); - boolean sessionExists = sessionRepository.existsById(id); - if (!sessionExists) { - new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); - } - - try { - Session session = sessionRepository.getSessionById(id); - ArrayList messagesList = session.getMessages(); - - Message firstMessage = messagesList.get(0); - return new ResponseEntity<>(firstMessage.getDateString(), headers, HttpStatus.OK); - } catch (Exception e) { - return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST); - } - } -} \ No newline at end of file +//package callhub.connect.use_case; +// +//import callhub.connect.data_access.SessionRepository; +//import callhub.connect.entities.Message; +//import callhub.connect.entities.Session; +//import org.springframework.http.HttpHeaders; +//import org.springframework.http.HttpStatus; +//import org.springframework.http.ResponseEntity; +//import org.springframework.web.bind.annotation.GetMapping; +//import org.springframework.web.bind.annotation.PathVariable; +//import org.springframework.web.bind.annotation.RequestMapping; +//import org.springframework.web.bind.annotation.RestController; +// +//import java.util.ArrayList; +// +//@RestController +//@RequestMapping("/email") +//public class EmailController_fromthepast { +// +// public SessionRepository sessionRepository; +// +// public EmailController_fromthepast(SessionRepository sessionRepository) { +// this.sessionRepository = sessionRepository; +// } +// +// /** +// * Retrieves the transcript of messages for a given session code. +// * +// * @param id The session code to identify the session. +// * @return A ResponseEntity containing the transcript of messages as a String. +// * If the session is not found, an error occurs. +// */ +// @GetMapping("/transcript/{id}") +// public ResponseEntity getTranscript(@PathVariable String id) { +// HttpHeaders headers = new HttpHeaders(); +// boolean sessionExists = sessionRepository.existsById(id); +// if (!sessionExists) { +// return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); +// } +// +// Session session = sessionRepository.getSessionById(id); +// ArrayList messagesList = session.getMessages(); +// +// StringBuilder transcript = new StringBuilder(); +// for (Message message : messagesList) { +// transcript.append(message.formattedMessage()).append("\n"); +// } +// return new ResponseEntity<>(transcript.toString(), headers, HttpStatus.OK); +// } +// +// /** +// * Retrieves the date messages were sent for a given session code, assuming all messages +// * were sent on the same day as the first message. +// * +// * @param id The session code to identify the session. +// * @return A ResponseEntity containing the date of messages as a String. +// * If the session is not found, an error occurs. +// */ +// @GetMapping("/date/{id}") +// public ResponseEntity getDate(@PathVariable String id) { +// HttpHeaders headers = new HttpHeaders(); +// boolean sessionExists = sessionRepository.existsById(id); +// if (!sessionExists) { +// new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); +// } +// +// try { +// Session session = sessionRepository.getSessionById(id); +// ArrayList messagesList = session.getMessages(); +// +// Message firstMessage = messagesList.get(0); +// return new ResponseEntity<>(firstMessage.getDateString(), headers, HttpStatus.OK); +// } catch (Exception e) { +// return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST); +// } +// } +//} \ No newline at end of file diff --git a/src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java b/src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java index 47eb6f0..e19c82a 100644 --- a/src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java +++ b/src/main/java/callhub/connect/use_case/email/EmailDataAccessInterface.java @@ -1,7 +1,11 @@ package callhub.connect.use_case.email; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + import java.util.HashMap; +@Service public interface EmailDataAccessInterface { String getTranscript(String id); diff --git a/src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java b/src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java index d97cd74..397b574 100644 --- a/src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java +++ b/src/main/java/callhub/connect/use_case/email/EmailInputBoundary.java @@ -1,7 +1,9 @@ package callhub.connect.use_case.email; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +@Service public interface EmailInputBoundary { ResponseEntity getTranscript(EmailInputData inputData); diff --git a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java index 51c41e5..12bf155 100644 --- a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java +++ b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java @@ -24,7 +24,6 @@ public EmailInteractor(EmailDataAccessInterface emailDataAccessObject, EmailOutp public ResponseEntity getTranscript(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); String transcript = emailDataAccessObject.getTranscript(inputData.getId()); - EmailOutputData outputData = new EmailOutputData(transcript); return new ResponseEntity<>(transcript, headers, HttpStatus.OK); } @@ -32,7 +31,6 @@ public ResponseEntity getTranscript(EmailInputData inputData) { public ResponseEntity getDate(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); String date = emailDataAccessObject.getDate(inputData.getId()); - EmailOutputData outputData = new EmailOutputData(date); return new ResponseEntity<>(date, headers, HttpStatus.OK); } } diff --git a/src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java b/src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java index dedeb22..7f54a97 100644 --- a/src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java +++ b/src/main/java/callhub/connect/use_case/email/EmailOutputBoundary.java @@ -1,5 +1,8 @@ package callhub.connect.use_case.email; +import org.springframework.stereotype.Service; + +@Service public interface EmailOutputBoundary { String getResponse(EmailOutputData outputData); } From 6a0838b3d925e97a5f89b0471ddd8db2e11dcb75 Mon Sep 17 00:00:00 2001 From: lucieyang1 Date: Sun, 3 Dec 2023 17:26:51 -0500 Subject: [PATCH 4/6] made tests for email, fixes based on tests --- .../data_access/EmailDataAccessObject.java | 14 +-- .../callhub/connect/entities/Session.java | 4 + .../use_case/EmailController_fromthepast.java | 77 ------------- .../use_case/email/EmailInteractor.java | 6 +- src/test/java/callhub/connect/EmailTests.java | 103 ++++++++++++++++++ 5 files changed, 116 insertions(+), 88 deletions(-) delete mode 100644 src/main/java/callhub/connect/use_case/EmailController_fromthepast.java create mode 100644 src/test/java/callhub/connect/EmailTests.java diff --git a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java index 9e32914..ce07360 100644 --- a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java +++ b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java @@ -40,14 +40,10 @@ public String getDate(String id) { return "This session is inactive or does not exist."; } - try { - Session session = sessionRepository.getSessionById(id); - ArrayList messagesList = session.getMessages(); - - Message firstMessage = messagesList.get(0); - return firstMessage.getDateString(); - } catch (Exception e) { - return e.getMessage(); - } + Session session = sessionRepository.getSessionById(id); + ArrayList messagesList = session.getMessages(); + + Message firstMessage = messagesList.get(0); + return firstMessage.getDateString(); } } diff --git a/src/main/java/callhub/connect/entities/Session.java b/src/main/java/callhub/connect/entities/Session.java index 252bb7c..d777e8f 100644 --- a/src/main/java/callhub/connect/entities/Session.java +++ b/src/main/java/callhub/connect/entities/Session.java @@ -27,6 +27,10 @@ public Session(boolean active, String code){ this.documents = new ArrayList(); } + public void setId(String id) { + this.id = id; + } + public String getId() { return id; } diff --git a/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java b/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java deleted file mode 100644 index 6c62bad..0000000 --- a/src/main/java/callhub/connect/use_case/EmailController_fromthepast.java +++ /dev/null @@ -1,77 +0,0 @@ -//package callhub.connect.use_case; -// -//import callhub.connect.data_access.SessionRepository; -//import callhub.connect.entities.Message; -//import callhub.connect.entities.Session; -//import org.springframework.http.HttpHeaders; -//import org.springframework.http.HttpStatus; -//import org.springframework.http.ResponseEntity; -//import org.springframework.web.bind.annotation.GetMapping; -//import org.springframework.web.bind.annotation.PathVariable; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.RestController; -// -//import java.util.ArrayList; -// -//@RestController -//@RequestMapping("/email") -//public class EmailController_fromthepast { -// -// public SessionRepository sessionRepository; -// -// public EmailController_fromthepast(SessionRepository sessionRepository) { -// this.sessionRepository = sessionRepository; -// } -// -// /** -// * Retrieves the transcript of messages for a given session code. -// * -// * @param id The session code to identify the session. -// * @return A ResponseEntity containing the transcript of messages as a String. -// * If the session is not found, an error occurs. -// */ -// @GetMapping("/transcript/{id}") -// public ResponseEntity getTranscript(@PathVariable String id) { -// HttpHeaders headers = new HttpHeaders(); -// boolean sessionExists = sessionRepository.existsById(id); -// if (!sessionExists) { -// return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); -// } -// -// Session session = sessionRepository.getSessionById(id); -// ArrayList messagesList = session.getMessages(); -// -// StringBuilder transcript = new StringBuilder(); -// for (Message message : messagesList) { -// transcript.append(message.formattedMessage()).append("\n"); -// } -// return new ResponseEntity<>(transcript.toString(), headers, HttpStatus.OK); -// } -// -// /** -// * Retrieves the date messages were sent for a given session code, assuming all messages -// * were sent on the same day as the first message. -// * -// * @param id The session code to identify the session. -// * @return A ResponseEntity containing the date of messages as a String. -// * If the session is not found, an error occurs. -// */ -// @GetMapping("/date/{id}") -// public ResponseEntity getDate(@PathVariable String id) { -// HttpHeaders headers = new HttpHeaders(); -// boolean sessionExists = sessionRepository.existsById(id); -// if (!sessionExists) { -// new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); -// } -// -// try { -// Session session = sessionRepository.getSessionById(id); -// ArrayList messagesList = session.getMessages(); -// -// Message firstMessage = messagesList.get(0); -// return new ResponseEntity<>(firstMessage.getDateString(), headers, HttpStatus.OK); -// } catch (Exception e) { -// return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST); -// } -// } -//} \ No newline at end of file diff --git a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java index 12bf155..d5f6d0c 100644 --- a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java +++ b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java @@ -24,13 +24,15 @@ public EmailInteractor(EmailDataAccessInterface emailDataAccessObject, EmailOutp public ResponseEntity getTranscript(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); String transcript = emailDataAccessObject.getTranscript(inputData.getId()); - return new ResponseEntity<>(transcript, headers, HttpStatus.OK); + EmailOutputData outputData = new EmailOutputData(transcript); + return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); } @Override public ResponseEntity getDate(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); String date = emailDataAccessObject.getDate(inputData.getId()); - return new ResponseEntity<>(date, headers, HttpStatus.OK); + EmailOutputData outputData = new EmailOutputData(date); + return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); } } diff --git a/src/test/java/callhub/connect/EmailTests.java b/src/test/java/callhub/connect/EmailTests.java new file mode 100644 index 0000000..3fec75f --- /dev/null +++ b/src/test/java/callhub/connect/EmailTests.java @@ -0,0 +1,103 @@ +package callhub.connect; + +import callhub.connect.data_access.SessionRepository; +import callhub.connect.entities.Message; +import callhub.connect.entities.Session; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDateTime; +import java.time.Month; + +import static callhub.connect.entities.Sender.CUSTOMER; +import static callhub.connect.entities.Sender.EMPLOYEE; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +class EmailTests { + + @MockBean + public SessionRepository sessionRepository; + + @Autowired + MockMvc mockMvc; + + @Test + void getTranscriptSuccess() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); + LocalDateTime timeStamp2 = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 11, 12); + Message message1 = new Message("Test Message", timeStamp, mockSession.getId(), CUSTOMER); + Message message2 = new Message("Reply Message", timeStamp2, mockSession.getId(), EMPLOYEE); + mockSession.addMessage(message1); + mockSession.addMessage(message2); + when(sessionRepository.existsById(anyString())).thenReturn(true); + when(sessionRepository.getSessionById(anyString())).thenReturn(mockSession); + String expected = "17:09:48 CUSTOMER: Test Message\n17:11:12 EMPLOYEE: Reply Message\n"; + + String Url = "/email/transcript/" + mockSession.getId(); + mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + } + + @Test + void getTranscriptDNE() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); + LocalDateTime timeStamp2 = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 11, 12); + Message message1 = new Message("Test Message", timeStamp, mockSession.getId(), CUSTOMER); + Message message2 = new Message("Reply Message", timeStamp2, mockSession.getId(), EMPLOYEE); + mockSession.addMessage(message1); + mockSession.addMessage(message2); + when(sessionRepository.existsById(anyString())).thenReturn(false); + String expected = "This session is inactive or does not exist."; + + String Url = "/email/transcript/" + mockSession.getId(); + mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + } + + @Test + void getCode() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); + LocalDateTime timeStamp2 = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 11, 12); + Message message1 = new Message("Test Message", timeStamp, mockSession.getId(), CUSTOMER); + Message message2 = new Message("Reply Message", timeStamp2, mockSession.getId(), EMPLOYEE); + mockSession.addMessage(message1); + mockSession.addMessage(message2); + when(sessionRepository.existsById(anyString())).thenReturn(true); + when(sessionRepository.getSessionById(anyString())).thenReturn(mockSession); + String expected = "December 03, 2023"; + + String Url = "/email/date/" + mockSession.getId(); + mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + } + + @Test + void getCodeDNE() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); + LocalDateTime timeStamp2 = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 11, 12); + Message message1 = new Message("Test Message", timeStamp, mockSession.getId(), CUSTOMER); + Message message2 = new Message("Reply Message", timeStamp2, mockSession.getId(), EMPLOYEE); + mockSession.addMessage(message1); + mockSession.addMessage(message2); + when(sessionRepository.existsById(anyString())).thenReturn(false); + String expected = "This session is inactive or does not exist."; + + String Url = "/email/date/" + mockSession.getId(); + mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + } +} \ No newline at end of file From f77c4eb58a764dcc8844ad86430c55ee6b6d5e36 Mon Sep 17 00:00:00 2001 From: lucieyang1 Date: Mon, 4 Dec 2023 00:32:33 -0500 Subject: [PATCH 5/6] fixed error handling --- .../data_access/EmailDataAccessObject.java | 44 +++++++++++++++---- .../exceptions/NoMessagesException.java | 7 +++ .../exceptions/SessionNotFoundException.java | 7 +++ .../use_case/email/EmailInteractor.java | 27 +++++++++--- src/test/java/callhub/connect/EmailTests.java | 40 ++++++++++++++--- 5 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java create mode 100644 src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java diff --git a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java index ce07360..2290d53 100644 --- a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java +++ b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java @@ -2,6 +2,8 @@ import callhub.connect.entities.Message; import callhub.connect.entities.Session; +import callhub.connect.entities.exceptions.NoMessagesException; +import callhub.connect.entities.exceptions.SessionNotFoundException; import callhub.connect.use_case.email.EmailDataAccessInterface; import org.springframework.stereotype.Service; @@ -15,35 +17,59 @@ public EmailDataAccessObject(SessionRepository sessionRepository) { this.sessionRepository = sessionRepository; } + /** + * Retrieves the transcript of messages for a given session code. + * + * @param id The session code to identify the session. + * @return A ResponseEntity containing the transcript of messages as a String. + * @throws NullPointerException if there is no session with the id in the sessionRepository. + */ @Override public String getTranscript(String id) { boolean sessionExists = sessionRepository.existsById(id); if (!sessionExists) { - return "This session is inactive or does not exist."; + throw new SessionNotFoundException("Session not found."); } Session session = sessionRepository.getSessionById(id); ArrayList messagesList = session.getMessages(); - StringBuilder transcript = new StringBuilder(); - for (Message message : messagesList) { - transcript.append(message.formattedMessage()).append("\n"); + if (!(messagesList.isEmpty())) { + StringBuilder transcript = new StringBuilder(); + for (Message message : messagesList) { + transcript.append(message.formattedMessage()).append("\n"); + } + return transcript.toString(); + } + else{ + throw new NoMessagesException("No messages found."); } - return transcript.toString(); - } + /** + * Retrieves the date messages were sent for a given session code, assuming all messages + * were sent on the same day as the first message. + * + * @param id The session code to identify the session. + * @return A ResponseEntity containing the date of messages as a String. + * @throws NullPointerException if there is no session with the id in the sessionRepository. + */ @Override public String getDate(String id) { boolean sessionExists = sessionRepository.existsById(id); if (!sessionExists) { - return "This session is inactive or does not exist."; + throw new SessionNotFoundException("Session not found."); } Session session = sessionRepository.getSessionById(id); ArrayList messagesList = session.getMessages(); - Message firstMessage = messagesList.get(0); - return firstMessage.getDateString(); + if (!(messagesList.isEmpty())) { + Message firstMessage = messagesList.get(0); + return firstMessage.getDateString(); + } + else { + throw new NoMessagesException("No messages found."); + } } } diff --git a/src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java b/src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java new file mode 100644 index 0000000..f1aca23 --- /dev/null +++ b/src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java @@ -0,0 +1,7 @@ +package callhub.connect.entities.exceptions; + +public class NoMessagesException extends RuntimeException { + public NoMessagesException(String message) { + super(message); + } +} diff --git a/src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java b/src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java new file mode 100644 index 0000000..2c40f00 --- /dev/null +++ b/src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java @@ -0,0 +1,7 @@ +package callhub.connect.entities.exceptions; + +public class SessionNotFoundException extends RuntimeException { + public SessionNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java index d5f6d0c..d8c579d 100644 --- a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java +++ b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java @@ -1,5 +1,7 @@ package callhub.connect.use_case.email; +import callhub.connect.entities.exceptions.NoMessagesException; +import callhub.connect.entities.exceptions.SessionNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -23,16 +25,29 @@ public EmailInteractor(EmailDataAccessInterface emailDataAccessObject, EmailOutp @Override public ResponseEntity getTranscript(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); - String transcript = emailDataAccessObject.getTranscript(inputData.getId()); - EmailOutputData outputData = new EmailOutputData(transcript); - return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + try { + String transcript = emailDataAccessObject.getTranscript(inputData.getId()); + EmailOutputData outputData = new EmailOutputData(transcript); + return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + } catch (SessionNotFoundException e) { + return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); + } catch (NoMessagesException e) { + return new ResponseEntity<>("This session has no messages.", headers, HttpStatus.NOT_FOUND); + } } + @Override public ResponseEntity getDate(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); - String date = emailDataAccessObject.getDate(inputData.getId()); - EmailOutputData outputData = new EmailOutputData(date); - return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + try { + String date = emailDataAccessObject.getDate(inputData.getId()); + EmailOutputData outputData = new EmailOutputData(date); + return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + } catch (SessionNotFoundException e) { + return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); + } catch (NoMessagesException e) { + return new ResponseEntity<>("This session has no messages.", headers, HttpStatus.NOT_FOUND); + } } } diff --git a/src/test/java/callhub/connect/EmailTests.java b/src/test/java/callhub/connect/EmailTests.java index 3fec75f..139249d 100644 --- a/src/test/java/callhub/connect/EmailTests.java +++ b/src/test/java/callhub/connect/EmailTests.java @@ -49,6 +49,20 @@ void getTranscriptSuccess() throws Exception { mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); } + @Test + void getTranscriptEmpty() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + + when(sessionRepository.existsById(anyString())).thenReturn(true); + when(sessionRepository.getSessionById(anyString())).thenReturn(mockSession); + + String Url = "/email/transcript/" + mockSession.getId(); + mockMvc.perform(get(Url)) .andExpect(status().isNotFound()) + .andExpect(content().string("This session has no messages.")); + } + + @Test void getTranscriptDNE() throws Exception { Session mockSession = new Session(true, "ABCDEF"); @@ -60,14 +74,14 @@ void getTranscriptDNE() throws Exception { mockSession.addMessage(message1); mockSession.addMessage(message2); when(sessionRepository.existsById(anyString())).thenReturn(false); - String expected = "This session is inactive or does not exist."; String Url = "/email/transcript/" + mockSession.getId(); - mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + mockMvc.perform(get(Url)).andExpect(status().isNotFound()) + .andExpect(content().string("This session is inactive or does not exist.")); } @Test - void getCode() throws Exception { + void getDateSuccess() throws Exception { Session mockSession = new Session(true, "ABCDEF"); mockSession.setId("abc123adsbksdhlsad"); LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); @@ -85,7 +99,21 @@ void getCode() throws Exception { } @Test - void getCodeDNE() throws Exception { + void getDateEmpty() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + + when(sessionRepository.existsById(anyString())).thenReturn(true); + when(sessionRepository.getSessionById(anyString())).thenReturn(mockSession); + String expected = ""; + + String Url = "/email/date/" + mockSession.getId(); + mockMvc.perform(get(Url)) .andExpect(status().isNotFound()) + .andExpect(content().string("This session has no messages.")); + } + + @Test + void getDateDNE() throws Exception { Session mockSession = new Session(true, "ABCDEF"); mockSession.setId("abc123adsbksdhlsad"); LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); @@ -95,9 +123,9 @@ void getCodeDNE() throws Exception { mockSession.addMessage(message1); mockSession.addMessage(message2); when(sessionRepository.existsById(anyString())).thenReturn(false); - String expected = "This session is inactive or does not exist."; String Url = "/email/date/" + mockSession.getId(); - mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + mockMvc.perform(get(Url)).andExpect(status().isNotFound()) + .andExpect(content().string("This session is inactive or does not exist.")); } } \ No newline at end of file From f56d72472645b7ccd90fee1fcbb3d34dbebf8705 Mon Sep 17 00:00:00 2001 From: lucieyang1 Date: Mon, 4 Dec 2023 00:32:33 -0500 Subject: [PATCH 6/6] fixed error handling --- .../data_access/EmailDataAccessObject.java | 44 +++++++++++++++---- .../exceptions/NoMessagesException.java | 7 +++ .../exceptions/SessionNotFoundException.java | 7 +++ .../use_case/email/EmailInteractor.java | 27 +++++++++--- src/test/java/callhub/connect/EmailTests.java | 39 +++++++++++++--- 5 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java create mode 100644 src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java diff --git a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java index ce07360..2290d53 100644 --- a/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java +++ b/src/main/java/callhub/connect/data_access/EmailDataAccessObject.java @@ -2,6 +2,8 @@ import callhub.connect.entities.Message; import callhub.connect.entities.Session; +import callhub.connect.entities.exceptions.NoMessagesException; +import callhub.connect.entities.exceptions.SessionNotFoundException; import callhub.connect.use_case.email.EmailDataAccessInterface; import org.springframework.stereotype.Service; @@ -15,35 +17,59 @@ public EmailDataAccessObject(SessionRepository sessionRepository) { this.sessionRepository = sessionRepository; } + /** + * Retrieves the transcript of messages for a given session code. + * + * @param id The session code to identify the session. + * @return A ResponseEntity containing the transcript of messages as a String. + * @throws NullPointerException if there is no session with the id in the sessionRepository. + */ @Override public String getTranscript(String id) { boolean sessionExists = sessionRepository.existsById(id); if (!sessionExists) { - return "This session is inactive or does not exist."; + throw new SessionNotFoundException("Session not found."); } Session session = sessionRepository.getSessionById(id); ArrayList messagesList = session.getMessages(); - StringBuilder transcript = new StringBuilder(); - for (Message message : messagesList) { - transcript.append(message.formattedMessage()).append("\n"); + if (!(messagesList.isEmpty())) { + StringBuilder transcript = new StringBuilder(); + for (Message message : messagesList) { + transcript.append(message.formattedMessage()).append("\n"); + } + return transcript.toString(); + } + else{ + throw new NoMessagesException("No messages found."); } - return transcript.toString(); - } + /** + * Retrieves the date messages were sent for a given session code, assuming all messages + * were sent on the same day as the first message. + * + * @param id The session code to identify the session. + * @return A ResponseEntity containing the date of messages as a String. + * @throws NullPointerException if there is no session with the id in the sessionRepository. + */ @Override public String getDate(String id) { boolean sessionExists = sessionRepository.existsById(id); if (!sessionExists) { - return "This session is inactive or does not exist."; + throw new SessionNotFoundException("Session not found."); } Session session = sessionRepository.getSessionById(id); ArrayList messagesList = session.getMessages(); - Message firstMessage = messagesList.get(0); - return firstMessage.getDateString(); + if (!(messagesList.isEmpty())) { + Message firstMessage = messagesList.get(0); + return firstMessage.getDateString(); + } + else { + throw new NoMessagesException("No messages found."); + } } } diff --git a/src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java b/src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java new file mode 100644 index 0000000..f1aca23 --- /dev/null +++ b/src/main/java/callhub/connect/entities/exceptions/NoMessagesException.java @@ -0,0 +1,7 @@ +package callhub.connect.entities.exceptions; + +public class NoMessagesException extends RuntimeException { + public NoMessagesException(String message) { + super(message); + } +} diff --git a/src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java b/src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java new file mode 100644 index 0000000..2c40f00 --- /dev/null +++ b/src/main/java/callhub/connect/entities/exceptions/SessionNotFoundException.java @@ -0,0 +1,7 @@ +package callhub.connect.entities.exceptions; + +public class SessionNotFoundException extends RuntimeException { + public SessionNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java index d5f6d0c..d8c579d 100644 --- a/src/main/java/callhub/connect/use_case/email/EmailInteractor.java +++ b/src/main/java/callhub/connect/use_case/email/EmailInteractor.java @@ -1,5 +1,7 @@ package callhub.connect.use_case.email; +import callhub.connect.entities.exceptions.NoMessagesException; +import callhub.connect.entities.exceptions.SessionNotFoundException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -23,16 +25,29 @@ public EmailInteractor(EmailDataAccessInterface emailDataAccessObject, EmailOutp @Override public ResponseEntity getTranscript(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); - String transcript = emailDataAccessObject.getTranscript(inputData.getId()); - EmailOutputData outputData = new EmailOutputData(transcript); - return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + try { + String transcript = emailDataAccessObject.getTranscript(inputData.getId()); + EmailOutputData outputData = new EmailOutputData(transcript); + return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + } catch (SessionNotFoundException e) { + return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); + } catch (NoMessagesException e) { + return new ResponseEntity<>("This session has no messages.", headers, HttpStatus.NOT_FOUND); + } } + @Override public ResponseEntity getDate(EmailInputData inputData) { HttpHeaders headers = new HttpHeaders(); - String date = emailDataAccessObject.getDate(inputData.getId()); - EmailOutputData outputData = new EmailOutputData(date); - return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + try { + String date = emailDataAccessObject.getDate(inputData.getId()); + EmailOutputData outputData = new EmailOutputData(date); + return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK); + } catch (SessionNotFoundException e) { + return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND); + } catch (NoMessagesException e) { + return new ResponseEntity<>("This session has no messages.", headers, HttpStatus.NOT_FOUND); + } } } diff --git a/src/test/java/callhub/connect/EmailTests.java b/src/test/java/callhub/connect/EmailTests.java index 3fec75f..95dfdab 100644 --- a/src/test/java/callhub/connect/EmailTests.java +++ b/src/test/java/callhub/connect/EmailTests.java @@ -49,6 +49,20 @@ void getTranscriptSuccess() throws Exception { mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); } + @Test + void getTranscriptEmpty() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + + when(sessionRepository.existsById(anyString())).thenReturn(true); + when(sessionRepository.getSessionById(anyString())).thenReturn(mockSession); + + String Url = "/email/transcript/" + mockSession.getId(); + mockMvc.perform(get(Url)) .andExpect(status().isNotFound()) + .andExpect(content().string("This session has no messages.")); + } + + @Test void getTranscriptDNE() throws Exception { Session mockSession = new Session(true, "ABCDEF"); @@ -60,14 +74,14 @@ void getTranscriptDNE() throws Exception { mockSession.addMessage(message1); mockSession.addMessage(message2); when(sessionRepository.existsById(anyString())).thenReturn(false); - String expected = "This session is inactive or does not exist."; String Url = "/email/transcript/" + mockSession.getId(); - mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + mockMvc.perform(get(Url)).andExpect(status().isNotFound()) + .andExpect(content().string("This session is inactive or does not exist.")); } @Test - void getCode() throws Exception { + void getDateSuccess() throws Exception { Session mockSession = new Session(true, "ABCDEF"); mockSession.setId("abc123adsbksdhlsad"); LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); @@ -85,7 +99,20 @@ void getCode() throws Exception { } @Test - void getCodeDNE() throws Exception { + void getDateEmpty() throws Exception { + Session mockSession = new Session(true, "ABCDEF"); + mockSession.setId("abc123adsbksdhlsad"); + + when(sessionRepository.existsById(anyString())).thenReturn(true); + when(sessionRepository.getSessionById(anyString())).thenReturn(mockSession); + + String Url = "/email/date/" + mockSession.getId(); + mockMvc.perform(get(Url)) .andExpect(status().isNotFound()) + .andExpect(content().string("This session has no messages.")); + } + + @Test + void getDateDNE() throws Exception { Session mockSession = new Session(true, "ABCDEF"); mockSession.setId("abc123adsbksdhlsad"); LocalDateTime timeStamp = LocalDateTime.of(2023, Month.DECEMBER, 3, 17, 9, 48); @@ -95,9 +122,9 @@ void getCodeDNE() throws Exception { mockSession.addMessage(message1); mockSession.addMessage(message2); when(sessionRepository.existsById(anyString())).thenReturn(false); - String expected = "This session is inactive or does not exist."; String Url = "/email/date/" + mockSession.getId(); - mockMvc.perform(get(Url)).andExpect(status().isOk()).andExpect(content().string(expected)); + mockMvc.perform(get(Url)).andExpect(status().isNotFound()) + .andExpect(content().string("This session is inactive or does not exist.")); } } \ No newline at end of file