### Question:
MailboxApiController extends AbstractApiController { public Result createMailbox(@JSR303Validation final MailboxData mailboxData, @Attribute("user") final User user, final Context context) { if (context.getValidation().hasViolations()) { return ApiResults.badRequest(context.getValidation().getViolations()); } return doCreateMailbox(mailboxData, user); }  Result listMailboxes(@Attribute("userId") @DbId final Long userId, final Context context); Result createMailbox(@JSR303Validation final MailboxData mailboxData, @Attribute("user") final User user,
                                final Context context); Result getMailbox(@PathParam("mailboxAddress") @Email final String mailboxAddress,
                             @Attribute("userId") @DbId final Long userId, final Context context); Result updateMailbox(@PathParam("mailboxAddress") @Email final String mailboxAddress,
                                @JSR303Validation final MailboxData mailboxData,
                                @Attribute("userId") @DbId final Long userId, final Context context); Result deleteMailbox(@PathParam("mailboxAddress") @Email final String mailboxAddress,
                                @Attribute("userId") @DbId final Long userId, final Context context);  }### Answer:
@Test public void createMailbox_invalidEmail() throws Exception { final HttpResponse response = apiClient.createMailbox("foo(at)bar.com", System.currentTimeMillis(), false); RestApiTestUtils.validateStatusCode(response, 400); RestApiTestUtils.validateErrors(response, "address"); }
@Test public void createMailbox_unknownMailDomain() throws Exception { final HttpResponse response = apiClient.createMailbox("foo@bar.com", System.currentTimeMillis(), false); RestApiTestUtils.validateStatusCode(response, 403); }
@Test public void createMailbox() throws Exception { final String mailboxAddress = "foo@xcmailr.test"; final long expirationDate = System.currentTimeMillis(); final boolean forwardEnabled = true; final HttpResponse response = apiClient.createMailbox(mailboxAddress, expirationDate, forwardEnabled); RestApiTestUtils.validateStatusCode(response, 201); final MailboxData mailboxData = RestApiTestUtils.getResponseBodyAs(response, MailboxData.class); RestApiTestUtils.validateMailboxData(mailboxData, mailboxAddress, expirationDate, forwardEnabled); MBox mailbox = MBox.getByAddress(mailboxData.address); RestApiTestUtils.validateMailbox(mailbox, mailboxData); }
@Test public void createMailbox_emailAlreadyTakenBySameUser() throws Exception { MBox mailbox = TestDataUtils.createMailbox(user); final String mailboxAddress = mailbox.getFullAddress(); final long expirationDate = System.currentTimeMillis(); final boolean forwardEnabled = true; final HttpResponse response = apiClient.createMailbox(mailboxAddress, expirationDate, forwardEnabled); RestApiTestUtils.validateStatusCode(response, 200); final MailboxData mailboxData = RestApiTestUtils.getResponseBodyAs(response, MailboxData.class); RestApiTestUtils.validateMailboxData(mailboxData, mailboxAddress, expirationDate, forwardEnabled); mailbox = MBox.getById(mailbox.getId()); RestApiTestUtils.validateMailbox(mailbox, mailboxData); }
@Test public void createMailbox_emailAlreadyTakenByOtherUser() throws Exception { final User otherUser = TestDataUtils.createUser(); final MBox otherUsersMailbox = TestDataUtils.createMailbox(otherUser); final String mailboxAddress = otherUsersMailbox.getFullAddress(); final long expirationDate = System.currentTimeMillis(); final boolean forwardEnabled = true; final HttpResponse response = apiClient.createMailbox(mailboxAddress, expirationDate, forwardEnabled); RestApiTestUtils.validateStatusCode(response, 403); }