@Test public void deletingAllMailsShouldCreateATask() throws Exception { mailRepositoryStore.create(URL_MY_REPO); when() .delete(PATH_ESCAPED_MY_REPO + "/mails") .then() .statusCode(HttpStatus.CREATED_201) .header("Location", is(notNullValue())) .body("taskId", is(notNullValue())); }
@Test public void getMailRepositoriesShouldDeduplicateAccordingToPath() throws Exception { mailRepositoryStore.create(URL_MY_REPO); mailRepositoryStore.create(URL_MY_REPO_OTHER); when() .get() .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(1)) .body("[0].repository", is(PATH_MY_REPO.asString())) .body("[0].path", is(PATH_ESCAPED_MY_REPO)); }
@Test public void reprocessingAllTaskShouldCreateATask() throws Exception { mailRepositoryStore.create(URL_MY_REPO); given() .param("action", "reprocess") .when() .patch(PATH_ESCAPED_MY_REPO + "/mails") .then() .statusCode(HttpStatus.CREATED_201) .header("Location", is(notNullValue())) .body("taskId", is(notNullValue())); }
@Test public void getMailRepositoriesShouldReturnRepositoryWhenOne() throws Exception { mailRepositoryStore.create(URL_MY_REPO); when() .get() .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(1)) .body("[0].repository", is(PATH_MY_REPO.asString())) .body("[0].path", is(PATH_ESCAPED_MY_REPO)); }
@Test public void listingKeysShouldReturnEmptyWhenNoMail() throws Exception { mailRepositoryStore.create(URL_MY_REPO); when() .get(MY_REPO_MAILS) .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(0)); }
@Test public void retrievingAMailShouldFailWhenUnknown() throws Exception { mailRepositoryStore.create(URL_MY_REPO); String name = "name"; when() .get(PATH_ESCAPED_MY_REPO + "/mails/" + name) .then() .statusCode(HttpStatus.NOT_FOUND_404) .body("statusCode", is(404)) .body("type", is(ErrorResponder.ErrorType.NOT_FOUND.getType())) .body("message", is("Could not retrieve " + name)); }
@Test public void deletingAMailShouldReturnOkWhenNotExist() throws Exception { mailRepositoryStore.create(URL_MY_REPO); when() .delete(PATH_ESCAPED_MY_REPO + "/mails/name") .then() .statusCode(HttpStatus.NO_CONTENT_204); }
@Test public void retrievingRepositorySizeShouldReturnZeroWhenEmpty() throws Exception { mailRepositoryStore.create(URL_MY_REPO); given() .get(PATH_ESCAPED_MY_REPO) .then() .statusCode(HttpStatus.OK_200) .contentType(ContentType.JSON) .body("size", equalTo(0)); }
@Test public void retrievingRepositoryShouldReturnBasicInformation() throws Exception { mailRepositoryStore.create(URL_MY_REPO); given() .get(PATH_ESCAPED_MY_REPO) .then() .statusCode(HttpStatus.OK_200) .contentType(ContentType.JSON) .body("repository", is(PATH_MY_REPO.asString())) .body("path", is(PATH_ESCAPED_MY_REPO)); }
@Test public void reprocessingOneTaskShouldCreateATask() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); given() .param("action", "reprocess") .when() .patch(PATH_ESCAPED_MY_REPO + "/mails/name1") .then() .statusCode(HttpStatus.CREATED_201) .header("Location", is(notNullValue())) .body("taskId", is(notNullValue())); }
@Test public void getMailRepositoriesShouldReturnTwoRepositoriesWhenTwo() throws Exception { mailRepositoryStore.create(URL_MY_REPO); mailRepositoryStore.create(MailRepositoryUrl.from("memory://mySecondRepo")); List<String> mailRepositories = when() .get() .then() .statusCode(HttpStatus.OK_200) .contentType(ContentType.JSON) .extract() .body() .jsonPath() .getList("repository"); assertThat(mailRepositories) .containsOnly(PATH_ESCAPED_MY_REPO, "mySecondRepo"); }
@Test public void reprocessingOneTaskShouldRequireAction() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); when() .patch(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .then() .statusCode(HttpStatus.BAD_REQUEST_400) .body("statusCode", is(400)) .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) .body("message", is("action query parameter is mandatory. The only supported value is `reprocess`")); }
@Test public void listingKeysShouldMergeRepositoryContentWhenSamePath() throws Exception { MailRepository mailRepository1 = mailRepositoryStore.create(URL_MY_REPO); MailRepository mailRepository2 = mailRepositoryStore.create(URL_MY_REPO_OTHER); mailRepository1.store(FakeMail.builder() .name("name1") .build()); mailRepository2.store(FakeMail.builder() .name("name2") .build()); when() .get(MY_REPO_MAILS) .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(2)) .body("", containsInAnyOrder("name1", "name2")); }
@Test public void reprocessingOneTaskShouldRejectInvalidActions() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); given() .param("action", "invalid") .when() .patch(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .then() .statusCode(HttpStatus.BAD_REQUEST_400) .body("statusCode", is(400)) .body("type", is(ErrorResponder.ErrorType.INVALID_ARGUMENT.getType())) .body("message", is("action query parameter is mandatory. The only supported value is `reprocess`")); }
@Test public void retrievingRepositorySizeShouldReturnNumberOfContainedMails() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); given() .get(PATH_ESCAPED_MY_REPO) .then() .statusCode(HttpStatus.OK_200) .contentType(ContentType.JSON) .body("size", equalTo(1)); }
@Test public void listingKeysShouldReturnContainedKeys() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name("name1") .build()); mailRepository.store(FakeMail.builder() .name("name2") .build()); when() .get(MY_REPO_MAILS) .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(2)) .body("", containsInAnyOrder("name1", "name2")); }
@Test public void downloadingAMailShouldFailWhenUnknown() throws Exception { mailRepositoryStore.create(URL_MY_REPO); RestAssured.requestSpecification = new RequestSpecBuilder().setPort(webAdminServer.getPort().getValue()) .setBasePath(MailRepositoriesRoutes.MAIL_REPOSITORIES) .build(); RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.TEXT); String name = "name"; given() .accept(Constants.RFC822_CONTENT_TYPE) .when() .get(PATH_ESCAPED_MY_REPO + "/mails/" + name) .then() .statusCode(HttpStatus.NOT_FOUND_404) .body("statusCode", is(404)) .body("type", is(ErrorResponder.ErrorType.NOT_FOUND.getType())) .body("message", is("Could not retrieve " + name)); }
@Test public void deletingAMailShouldReturnOkWhenExist() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); when() .delete(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .then() .statusCode(HttpStatus.NO_CONTENT_204); }
@Test public void retrievingAMailShouldNotFailWhenOnlyNameProperty() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); when() .get(PATH_ESCAPED_MY_REPO + "/mails/" + NAME_1) .then() .statusCode(HttpStatus.OK_200) .body("name", is(NAME_1)) .body("sender", isEmptyOrNullString()) .body("state", isEmptyOrNullString()) .body("error", isEmptyOrNullString()) .body("recipients", hasSize(0)); }
@Test public void listingKeysShouldIgnoreZeroedOffset() throws Exception { MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); mailRepository.store(FakeMail.builder() .name(NAME_1) .build()); mailRepository.store(FakeMail.builder() .name(NAME_2) .build()); given() .param("offset", "0") .when() .get(MY_REPO_MAILS) .then() .statusCode(HttpStatus.OK_200) .body("", hasSize(2)) .body("", containsInAnyOrder(NAME_1, NAME_2)); }