SetAnnotationCommandParser extends AbstractImapCommandParser { @Override protected ImapMessage decode(ImapRequestLineReader request, Tag tag, ImapSession session) throws DecodingException { String mailboxName = request.mailbox(); ImmutableList.Builder<MailboxAnnotation> listMailboxAnnotations = ImmutableList.<MailboxAnnotation>builder(); if (request.nextWordChar() == '(') { request.consumeChar('('); do { listMailboxAnnotations.add(readNextAnnotation(request)); } while (request.nextWordChar() != ')'); request.consumeChar(')'); } request.eol(); return new SetAnnotationRequest(tag, mailboxName, listMailboxAnnotations.build()); } SetAnnotationCommandParser(StatusResponseFactory statusResponseFactory);   }
@Test public void decodeMessageShouldReturnRequestContainsOneAnnotation() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment \"This is my comment\") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(lineReader, TAG, null); assertThat(request.getMailboxName()).isEqualTo(INBOX); assertThat(request.getMailboxAnnotations()).containsOnly(PRIVATE_ANNOTATION); }
@Test(expected = DecodingException.class) public void decodeMessageShouldThrowDecodingExceptionWhenContainsInvalidAnnotationKey() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/pri*vate/comment \"This is my comment\") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); parser.decode(lineReader, TAG, null); }
@Test public void decodeMessageShouldReturnRequestContainsOneNilAnnotation() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment NIL) \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(lineReader, TAG, null); assertThat(request.getMailboxName()).isEqualTo(INBOX); assertThat(request.getMailboxAnnotations()).containsOnly(NIL_ANNOTATION); }
@Test public void decodeMessageShouldReturnRequestContainsOneAnnotationWithMultiLinesValue() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment {32}\nMy new comment across two lines.) \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, new ByteArrayOutputStream()); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(lineReader, TAG, null); assertThat(request.getMailboxName()).isEqualTo(INBOX); assertThat(request.getMailboxAnnotations()).containsOnly(MailboxAnnotation.newInstance(PRIVATE_KEY, "My new comment across two lines.")); }
@Test public void decodeMessageShouldReturnRequestContainsMultiAnnotations() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment \"This is my comment\" /shared/comment \"This one is for you!\") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, new ByteArrayOutputStream()); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(lineReader, TAG, null); assertThat(request.getMailboxName()).isEqualTo(INBOX); assertThat(request.getMailboxAnnotations()).containsExactly(PRIVATE_ANNOTATION, SHARED_ANNOTATION); }
@Test public void decodeMessageShouldReturnRequestContainsMultiAnnotationsWithNil() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment NIL /shared/comment \"This one is for you!\") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, new ByteArrayOutputStream()); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(lineReader, TAG, null); assertThat(request.getMailboxName()).isEqualTo(INBOX); assertThat(request.getMailboxAnnotations()).containsExactly(NIL_ANNOTATION, SHARED_ANNOTATION); }
@Test(expected = DecodingException.class) public void decodeMessageShouldThrowDecodingExceptionWhenCommandDoesNotStartWithSlash() throws DecodingException { InputStream inputStream = new ByteArrayInputStream("INBOX /private/comment \"This is my comment\") \n".getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); parser.decode(lineReader, TAG, null); }
@Test(expected = DecodingException.class) public void decodeMessageShouldThrowDecodingExceptionWhenCommandDoesNotEndWithSlash() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment \"This is my comment\" \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); parser.decode(lineReader, TAG, null); }
@Test(expected = DecodingException.class) public void decodeMessageShouldThrowDecodingExceptionWhenCommandDoesNotHaveAnnotationValue() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment) \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); parser.decode(lineReader, TAG, null); }
@Test public void decodeMessageShouldReturnRequestWhenCommandHasEmptyAnnotationValue() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment \" \") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(lineReader, TAG, null); assertThat(request.getMailboxAnnotations().get(0).getValue()).isPresent(); }
@Test(expected = DecodingException.class) public void decodeMessageShouldThrowDecodingExceptionWhenCommandAnnotationValueNotInQuote() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment This is my comment) \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); parser.decode(lineReader, TAG, null); }
@Test public void decodeMessageShouldReturnRequestWhenCommandAnnotationValueIsNILString() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " (/private/comment \"NIL\") \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); SetAnnotationRequest request = (SetAnnotationRequest) parser.decode(lineReader, TAG, null); assertThat(request.getMailboxAnnotations().get(0).getValue()).isPresent(); }
@Test(expected = DecodingException.class) public void decodeMessageShouldThrowDecodingExceptionWhenCommandMissingAnnotations() throws DecodingException { InputStream inputStream = new ByteArrayInputStream((INBOX + " () \n").getBytes()); ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, null); parser.decode(lineReader, TAG, null); }