mardi 31 mars 2020

JavaMail Unit Test for Downloading Email Attachments


I have a method that connects to an outlook mail server, filters the inbox based on today's date and then downloads all of the attachments from those emails.
I am trying to write a unit test for this method but do not know where to begin. What exactly should I be testing with regards to my method and how would you suggest I go about doing the unit testing for this method?
Below is the method I want to test.

try {
        // Creating mail session.
        Session session = Session.getDefaultInstance(props, auth);

        // Get the POP3 store provider and connect to the store.
        store = session.getStore(PROTOCOL);
        store.connect(HOST, Username, Password);

        // Get folder and open the INBOX folder in the store.
        inbox = store.getFolder("INBOX");
        inbox.open(Folder.READ_ONLY);
        }
        catch(Exception e) {
            log.warn("Failed to connect to mailbox",e);
    }

try {
    //Searching the inbox for the specified search condition above
        Message[] messages = inbox.search(searchCondition);

        for (int i = 0; i < messages.length; i++) {

            Message message = messages[i];
            String subject = message.getSubject();
            String contentType = message.getContentType();
            String messageContent = "";
            String attachFiles = "";

           //The messages have to have the correct date for the attachments to be downloaded (correct date is today's date). 
            if(message.getSentDate().toString().contains(formattedDate) && message.getSentDate().toString().contains(formattedYear)) {
                System.out.println("Found message #" + i + ": " + subject);
                 Multipart multiPart = (Multipart) message.getContent();
                 int numberOfParts = multiPart.getCount();
                for (int partCount = 0; partCount < numberOfParts; partCount++) {

                    MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
                    if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                        // this part is attachment
                        String fileName = part.getFileName();
                        attachFiles += fileName + ", ";
                        part.saveFile(saveDirectory + File.separator + fileName);
                        } 
                    else {
                        // this part may be the message content
                        messageContent = part.getContent().toString();
                    }
                }

                if (attachFiles.length() > 1) {
                    attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
                }
            } else if (contentType.contains("text/plain") || contentType.contains("text/html")) {
                Object content = message.getContent();
                if (content != null) {
                    messageContent = content.toString();
                }
            }
        }
   }catch(Exception e) {
       log.error("Failed to download attachments from email server",e);
   }

Aucun commentaire:

Enregistrer un commentaire