@Test public void testGetMailsForRESTAll() throws MessagingException, IOException { Message[] messages = createMockMessages(10, 5); when(this.inboxMock.getMessages(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())).thenReturn(messages); MessageDTO[] transferMessages = this.emailLogic.getMailsForREST(-1); for (int i = 0; i < transferMessages.length; i++) { compareMessages(messages[messages.length - 1 - i], transferMessages[i]); } }
public MessageDTO[] getMailsForREST(int amount) { Message[] messages; MessageDTO[] messagesToSend; try (Folder inbox = this.mailSession.getInbox()) { int amountInInbox = inbox.getMessageCount(); int lowerIndex = (amount == -1) ? 1 : Math.max(amountInInbox - amount, 1); messages = inbox.getMessages(lowerIndex, amountInInbox); messagesToSend = new MessageDTO[messages.length]; for (int i = 0; i < messages.length; i++) { Message m = messages[messages.length - 1 - i]; LocalDateTime sentDate = m.getSentDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); messagesToSend[i] = new MessageDTO(getFrom(m), m.getSubject(), sentDate, isImportantMessage(m), m.isSet(Flag.SEEN)); } return messagesToSend; } catch (MessagingException e) { this.logger.error("There were problems handling the messages", e); return new MessageDTO[0]; } }
EMailLogic { public MessageDTO[] getMailsForREST(int amount) { Message[] messages; MessageDTO[] messagesToSend; try (Folder inbox = this.mailSession.getInbox()) { int amountInInbox = inbox.getMessageCount(); int lowerIndex = (amount == -1) ? 1 : Math.max(amountInInbox - amount, 1); messages = inbox.getMessages(lowerIndex, amountInInbox); messagesToSend = new MessageDTO[messages.length]; for (int i = 0; i < messages.length; i++) { Message m = messages[messages.length - 1 - i]; LocalDateTime sentDate = m.getSentDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); messagesToSend[i] = new MessageDTO(getFrom(m), m.getSubject(), sentDate, isImportantMessage(m), m.isSet(Flag.SEEN)); } return messagesToSend; } catch (MessagingException e) { this.logger.error("There were problems handling the messages", e); return new MessageDTO[0]; } } }
EMailLogic { public MessageDTO[] getMailsForREST(int amount) { Message[] messages; MessageDTO[] messagesToSend; try (Folder inbox = this.mailSession.getInbox()) { int amountInInbox = inbox.getMessageCount(); int lowerIndex = (amount == -1) ? 1 : Math.max(amountInInbox - amount, 1); messages = inbox.getMessages(lowerIndex, amountInInbox); messagesToSend = new MessageDTO[messages.length]; for (int i = 0; i < messages.length; i++) { Message m = messages[messages.length - 1 - i]; LocalDateTime sentDate = m.getSentDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); messagesToSend[i] = new MessageDTO(getFrom(m), m.getSubject(), sentDate, isImportantMessage(m), m.isSet(Flag.SEEN)); } return messagesToSend; } catch (MessagingException e) { this.logger.error("There were problems handling the messages", e); return new MessageDTO[0]; } }  }
EMailLogic { public MessageDTO[] getMailsForREST(int amount) { Message[] messages; MessageDTO[] messagesToSend; try (Folder inbox = this.mailSession.getInbox()) { int amountInInbox = inbox.getMessageCount(); int lowerIndex = (amount == -1) ? 1 : Math.max(amountInInbox - amount, 1); messages = inbox.getMessages(lowerIndex, amountInInbox); messagesToSend = new MessageDTO[messages.length]; for (int i = 0; i < messages.length; i++) { Message m = messages[messages.length - 1 - i]; LocalDateTime sentDate = m.getSentDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); messagesToSend[i] = new MessageDTO(getFrom(m), m.getSubject(), sentDate, isImportantMessage(m), m.isSet(Flag.SEEN)); } return messagesToSend; } catch (MessagingException e) { this.logger.error("There were problems handling the messages", e); return new MessageDTO[0]; } }  boolean hasNewMessages(boolean checkForImportant); int getNewMessageCount(boolean checkForImportant); String printMessages(int amount, boolean important); MessageDTO[] getMailsForREST(int amount); boolean connectToMailServer(EMailCredentials credentials); boolean isConnectedToMailServer(); void disconnectFromMailServer(); EMailCredentials getCredentials(); @PostConstruct void connectToMailServer(); }
EMailLogic { public MessageDTO[] getMailsForREST(int amount) { Message[] messages; MessageDTO[] messagesToSend; try (Folder inbox = this.mailSession.getInbox()) { int amountInInbox = inbox.getMessageCount(); int lowerIndex = (amount == -1) ? 1 : Math.max(amountInInbox - amount, 1); messages = inbox.getMessages(lowerIndex, amountInInbox); messagesToSend = new MessageDTO[messages.length]; for (int i = 0; i < messages.length; i++) { Message m = messages[messages.length - 1 - i]; LocalDateTime sentDate = m.getSentDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); messagesToSend[i] = new MessageDTO(getFrom(m), m.getSubject(), sentDate, isImportantMessage(m), m.isSet(Flag.SEEN)); } return messagesToSend; } catch (MessagingException e) { this.logger.error("There were problems handling the messages", e); return new MessageDTO[0]; } }  boolean hasNewMessages(boolean checkForImportant); int getNewMessageCount(boolean checkForImportant); String printMessages(int amount, boolean important); MessageDTO[] getMailsForREST(int amount); boolean connectToMailServer(EMailCredentials credentials); boolean isConnectedToMailServer(); void disconnectFromMailServer(); EMailCredentials getCredentials(); @PostConstruct void connectToMailServer();  }