### Question:
MailProtocol { public SendEmailSyncRequest encodeRequest(MailRequest request) throws QuotaExceededException { return new SendEmailSyncRequest.Builder() .parameters(buildParametersMap(request)) .inputStream(buildStreamData(request.getMailContent())) .build(); } @Inject  MailProtocol(OpushEmailConfiguration emailConfiguration); MailRequest getRequest(ActiveSyncRequest request); Document encodeErrorResponse(String namespace, MailRequestStatus requestStatus); SendEmailSyncRequest encodeRequest(MailRequest request);  }### Answer:
@Test public void testEncodeRequest() throws Exception { OpushEmailConfiguration emailConfiguration = createMock(OpushEmailConfiguration.class); byte[] mailContent = new byte[] {123, 54, 23, 87, 10, 23, 10, 23 }; MailRequest sendSimpleEmailRequest = new MailRequest(CollectionId.of(23), CollectionId.of(23).serverId(12), true, mailContent); ActiveSyncRequest expectedActiveSyncRequest = new SendEmailSyncRequest.Builder() .parameters(ImmutableMap.<String, Object>of("CollectionId", CollectionId.of(23), "ItemId", CollectionId.of(23).serverId(12), "SaveInSent", "T")) .inputStream(new ByteArrayInputStream(mailContent)) .build(); expect(emailConfiguration.getMessageMaxSize()).andReturn(1024).once(); replay(emailConfiguration); MailProtocol mailProtocol = new MailProtocol(emailConfiguration); ActiveSyncRequest encodedRequest = mailProtocol.encodeRequest(sendSimpleEmailRequest); verify(emailConfiguration); assertThat(encodedRequest).isEqualTo(expectedActiveSyncRequest); }
@Test(expected=QuotaExceededException.class) public void testEncodeRequestMaxSizeException() throws Exception { OpushEmailConfiguration emailConfiguration = createMock(OpushEmailConfiguration.class); MailRequest sendSimpleEmailRequest = new MailRequest(CollectionId.of(23), CollectionId.of(23).serverId(12), true, new byte[] {123, 54, 23, 87, 10, 23, 10, 23 }); expect(emailConfiguration.getMessageMaxSize()).andReturn(2).once(); replay(emailConfiguration); MailProtocol mailProtocol = new MailProtocol(emailConfiguration); mailProtocol.encodeRequest(sendSimpleEmailRequest); }
@Test public void testEncodeRequestEmptyMailContent() throws Exception { MailRequest sendSimpleEmailRequest = new MailRequest(CollectionId.of(23), CollectionId.of(23).serverId(12), true, null); MailProtocol mailProtocol = new MailProtocol(null); ActiveSyncRequest encodedRequest = mailProtocol.encodeRequest(sendSimpleEmailRequest); ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) encodedRequest.getInputStream(); byte[] bytes = new byte[byteArrayInputStream.available()]; byteArrayInputStream.read(bytes); assertThat(bytes).isEmpty(); }