public static String getSystemResourceAsString(String filename) { return getSystemResourceAsString(filename, StandardCharsets.US_ASCII); }
public static String getSystemResourceAsString(String filename) { return getSystemResourceAsString(filename, StandardCharsets.US_ASCII); }
public static String getSystemResourceAsString(String filename) { return getSystemResourceAsString(filename, StandardCharsets.US_ASCII); }
@Test public void retrieveMessageShouldReturnTheMessageWhenMailExists() throws Exception { when(mailRepositoryStore.getByPath(FIRST_REPOSITORY_PATH)).thenReturn(Stream.of(repository)); FakeMail mail = FakeMail.builder() .name(NAME_1.asString()) .fileName("mail.eml") .build(); repository.store(mail); Optional<MimeMessage> mimeMessage = testee.retrieveMessage(FIRST_REPOSITORY_PATH, NAME_1); assertThat(mimeMessage).isNotEmpty(); String eml = IOUtils.toString(new MimeMessageInputStream(mimeMessage.get()), StandardCharsets.UTF_8); String expectedContent = ClassLoaderUtils.getSystemResourceAsString("mail.eml"); assertThat(eml).isEqualToNormalizingNewlines(expectedContent); } }
@Test public void quotaRatioShouldBeWellConvertedToJson() throws IOException { String user = "user@domain.org"; QuotaUsageUpdatedEvent event = new QuotaUsageUpdatedEvent( new MockMailboxSession(user), QuotaRoot.quotaRoot("any", Optional.of(Domain.of("domain.org"))), QuotaFixture.Counts._52_PERCENT, QuotaFixture.Sizes._55_PERCENT, Instant.now()); QuotaRatioToElasticSearchJson quotaRatioToElasticSearchJson = new QuotaRatioToElasticSearchJson(); String convertToJson = quotaRatioToElasticSearchJson.convertToJson(user, event); assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("quotaRatio.json")); }
@Test public void quotaRatioShouldBeWellConvertedToJsonWhenNoDomain() throws IOException { String user = "user"; QuotaUsageUpdatedEvent event = new QuotaUsageUpdatedEvent( new MockMailboxSession(user), QuotaRoot.quotaRoot("any", Optional.empty()), QuotaFixture.Counts._52_PERCENT, QuotaFixture.Sizes._55_PERCENT, Instant.now()); QuotaRatioToElasticSearchJson quotaRatioToElasticSearchJson = new QuotaRatioToElasticSearchJson(); String convertToJson = quotaRatioToElasticSearchJson.convertToJson(user, event); assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("quotaRatioNoDomain.json")); } }
@Given("^\"([^\"]*)\" receives a SMTP message specified in file \"([^\"]*)\" as message \"([^\"]*)\"$") public void smtpSend(String user, String fileName, String messageName) throws Exception { MailboxId mailboxId = mainStepdefs.mailboxProbe.getMailboxId(MailboxConstants.USER_NAMESPACE, user, INBOX); SMTPMessageSender smtpMessageSender = new SMTPMessageSender("domain.com"); smtpMessageSender .connect("127.0.0.1", mainStepdefs.jmapServer.getProbe(SmtpGuiceProbe.class).getSmtpPort()) .sendMessageWithHeaders("from@domain.com", user, ClassLoaderUtils.getSystemResourceAsString(fileName)); smtpMessageSender.close(); TestingConstants.calmlyAwait.until(() -> !retrieveIds(user, mailboxId).isEmpty()); List<String> ids = retrieveIds(user, mailboxId); messageIdStepdefs.addMessageId(messageName, mainStepdefs.messageIdFactory.fromString(ids.get(0))); }
@Test public void spamEmailShouldBeWellConvertedToJsonWithApacheTika() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( textExtractor, ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/nonTextual.eml"), new Flags(), propertyBuilder, MAILBOX_ID); spamMail.setUid(UID); spamMail.setModSeq(MOD_SEQ); assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo( ClassLoaderUtils.getSystemResourceAsString("eml/nonTextual.json", StandardCharsets.UTF_8)); }
@Test public void spamEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage spamMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/spamMail.eml"), new Flags(), propertyBuilder, MAILBOX_ID); spamMail.setUid(UID); spamMail.setModSeq(MOD_SEQ); assertThatJson(messageToElasticSearchJson.convertToJson(spamMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/spamMail.json")); }
@Test public void pgpSignedEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage pgpSignedMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/pgpSignedMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); pgpSignedMail.setModSeq(MOD_SEQ); pgpSignedMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(pgpSignedMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/pgpSignedMail.json")); }
@Test public void htmlEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage htmlMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/htmlMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("social", "pocket-money").build(), propertyBuilder, MAILBOX_ID); htmlMail.setModSeq(MOD_SEQ); htmlMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(htmlMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/htmlMail.json")); }
@Test public void emailWithNoInternalDateShouldUseNowDate() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test public void recursiveEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage recursiveMail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); recursiveMail.setModSeq(MOD_SEQ); recursiveMail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(recursiveMail, ImmutableList.of(MAILBOX_SESSION.getUser()))) .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test public void emailWithAttachmentsShouldConvertAttachmentsWhenIndexAttachmentsIsTrue() throws IOException { // Given MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); // When MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(MAILBOX_SESSION.getUser())); // Then assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMail.json")); }
@Test public void emailWithAttachmentsShouldNotConvertAttachmentsWhenIndexAttachmentsIsFalse() throws IOException { // Given MailboxMessage mailWithNoInternalDate = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/recursiveMail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mailWithNoInternalDate.setModSeq(MOD_SEQ); mailWithNoInternalDate.setUid(UID); // When MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.NO); String convertToJson = messageToElasticSearchJson.convertToJson(mailWithNoInternalDate, ImmutableList.of(MAILBOX_SESSION.getUser())); // Then assertThatJson(convertToJson) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/recursiveMailWithoutAttachments.json")); }
@Test public void simpleEmailShouldBeWellConvertedToJson() throws IOException { MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.YES); MailboxMessage mail = new SimpleMailboxMessage(MESSAGE_ID, date, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/mail.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); mail.setModSeq(MOD_SEQ); mail.setUid(UID); assertThatJson(messageToElasticSearchJson.convertToJson(mail, ImmutableList.of(new MockMailboxSession("user1").getUser(), new MockMailboxSession("user2").getUser()))) .when(IGNORING_ARRAY_ORDER).when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/mail.json")); }
@Test public void convertToJsonWithoutAttachmentShouldConvertEmailBoby() throws IOException { // Given MailboxMessage message = new SimpleMailboxMessage(MESSAGE_ID, null, SIZE, BODY_START_OCTET, ClassLoaderUtils.getSystemResourceAsSharedStream("eml/emailWithNonIndexableAttachment.eml"), new FlagsBuilder().add(Flags.Flag.DELETED, Flags.Flag.SEEN).add("debian", "security").build(), propertyBuilder, MAILBOX_ID); message.setModSeq(MOD_SEQ); message.setUid(UID); // When MessageToElasticSearchJson messageToElasticSearchJson = new MessageToElasticSearchJson( new DefaultTextExtractor(), ZoneId.of("Europe/Paris"), IndexAttachments.NO); String convertToJsonWithoutAttachment = messageToElasticSearchJson.convertToJsonWithoutAttachment(message, ImmutableList.of(MAILBOX_SESSION.getUser())); // Then assertThatJson(convertToJsonWithoutAttachment) .when(IGNORING_ARRAY_ORDER) .when(IGNORING_VALUES) .isEqualTo(ClassLoaderUtils.getSystemResourceAsString("eml/emailWithNonIndexableAttachmentWithoutAttachment.json")); } }
@Test public void downloadingAMailShouldReturnTheEml() throws Exception { RestAssured.requestSpecification = new RequestSpecBuilder().setPort(webAdminServer.getPort().getValue()) .setBasePath(MailRepositoriesRoutes.MAIL_REPOSITORIES) .build(); RestAssured.registerParser(Constants.RFC822_CONTENT_TYPE, Parser.TEXT); MailRepository mailRepository = mailRepositoryStore.create(URL_MY_REPO); String name = NAME_1; FakeMail mail = FakeMail.builder() .name(name) .fileName("mail.eml") .build(); mailRepository.store(mail); String expectedContent = ClassLoaderUtils.getSystemResourceAsString("mail.eml", StandardCharsets.UTF_8); String actualContent = given() .accept(Constants.RFC822_CONTENT_TYPE) .when() .get(PATH_ESCAPED_MY_REPO + "/mails/" + name) .then() .statusCode(HttpStatus.OK_200) .header("Content-Length", "471") .contentType(Constants.RFC822_CONTENT_TYPE) .extract() .body() .asString(); assertThat(actualContent).isEqualToNormalizingNewlines(expectedContent); }