Search in sources :

Example 1 with DeliveryStatus

use of org.motechproject.email.domain.DeliveryStatus in project motech by motech.

the class EmailAuditServiceBundleIT method shouldRetrieveEmailRecordWithSearchCriteria.

@Test
public void shouldRetrieveEmailRecordWithSearchCriteria() {
    emailRecordService.create(createEmailRecord("to1@address", "s 1", DeliveryStatus.SENT));
    emailRecordService.create(createEmailRecord("to@address2", "s 2", DeliveryStatus.SENT));
    Set<DeliveryStatus> deliveryStatuses = new HashSet<>();
    deliveryStatuses.add(DeliveryStatus.SENT);
    EmailRecordSearchCriteria criteriaToAddress = new EmailRecordSearchCriteria().withToAddress("to@address").withDeliveryStatuses(deliveryStatuses);
    List<EmailRecord> emailRecordsToAddress = emailAuditService.findEmailRecords(criteriaToAddress);
    assertNotNull(emailRecordsToAddress);
    assertEquals(1, emailRecordsToAddress.size());
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with DeliveryStatus

use of org.motechproject.email.domain.DeliveryStatus in project motech by motech.

the class EmailRecordServiceBundleIT method shouldCreateEmail.

@Test
public void shouldCreateEmail() {
    DeliveryStatus deliveryStatus = DeliveryStatus.SENT;
    String fromAddress = "f@adr";
    String toAddress = "t@adr";
    String subject = "test-subject";
    String message = "test-message";
    DateTime sentDate = DateUtil.now();
    EmailRecord emailRecord = new EmailRecord(fromAddress, toAddress, subject, message, sentDate, deliveryStatus);
    emailRecordService.create(emailRecord);
    List<EmailRecord> savedMessages = emailRecordService.findByRecipientAddress(toAddress);
    assertNotNull(savedMessages);
    assertEquals(asList(subject), extract(savedMessages, on(EmailRecord.class).getSubject()));
    assertEquals(asList(message), extract(savedMessages, on(EmailRecord.class).getMessage()));
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 3 with DeliveryStatus

use of org.motechproject.email.domain.DeliveryStatus in project motech by motech.

the class EmailRecordServiceBundleIT method shouldCreateIdenticalMessages.

@Test
public void shouldCreateIdenticalMessages() {
    DeliveryStatus deliveryStatus = DeliveryStatus.SENT;
    String fromAddress = "f@adr";
    String toAddress = "t@adr";
    String subject = "test-subject";
    String message = "test-message";
    DateTime messageTime = DateUtil.now().toDateTime(DateTimeZone.UTC);
    EmailRecord emailRecord = new EmailRecord(fromAddress, toAddress, subject, message, messageTime, deliveryStatus);
    emailRecordService.create(emailRecord);
    EmailRecord duplicateMessage = new EmailRecord(fromAddress, toAddress, subject, message, messageTime, deliveryStatus);
    emailRecordService.create(duplicateMessage);
    List<EmailRecord> allMessages = emailRecordService.findByRecipientAddress(toAddress);
    assertEquals(2, allMessages.size());
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with DeliveryStatus

use of org.motechproject.email.domain.DeliveryStatus in project motech by motech.

the class EmailRecordServiceBundleIT method shouldAddAndUpdateEmail.

@Test
public void shouldAddAndUpdateEmail() {
    DeliveryStatus deliveryStatus = DeliveryStatus.SENT;
    String fromAddress = "f@adr";
    String toAddress = "t@adr";
    String subject = "test-subject";
    String message = "test-message";
    DateTime messageTime = DateUtil.now().toDateTime(DateTimeZone.UTC);
    EmailRecord expected = new EmailRecord(fromAddress, toAddress, subject, message, messageTime, deliveryStatus);
    emailRecordService.create(expected);
    List<EmailRecord> emailRecords = emailRecordService.retrieveAll();
    assertEquals(asList(expected), emailRecords);
    EmailRecord actual = emailRecords.get(0);
    actual.setMessage("test-newmessage");
    emailRecordService.update(actual);
    emailRecords = emailRecordService.retrieveAll();
    assertEquals(asList(actual), emailRecords);
}
Also used : EmailRecord(org.motechproject.email.domain.EmailRecord) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 5 with DeliveryStatus

use of org.motechproject.email.domain.DeliveryStatus in project motech by motech.

the class AbstractSearchExecution method execute.

@Override
public T execute(Query query, InstanceSecurityRestriction restriction) {
    List<Property> properties = new ArrayList<>();
    Range<DateTime> deliveryTimeRange = criteria.getDeliveryTimeRange();
    boolean dateRangeUsed = isUsableRange(deliveryTimeRange);
    if (dateRangeUsed) {
        properties.add(new RangeProperty<>("deliveryTime", deliveryTimeRange, DateTime.class.getName()));
    }
    Set<DeliveryStatus> deliveryStatuses = criteria.getDeliveryStatuses();
    if (deliveryStatuses.isEmpty()) {
        deliveryStatuses = new HashSet<>(Arrays.asList(DeliveryStatus.values()));
    }
    properties.add(new SetProperty<>("deliveryStatus", deliveryStatuses, DeliveryStatus.class.getName()));
    StringBuilder queryBuilder = new StringBuilder(initialQuery(dateRangeUsed));
    int initialLength = queryBuilder.length();
    if (StringUtils.isNotEmpty(criteria.getToAddress())) {
        properties.add(new MatchesProperty("toAddress", criteria.getToAddress()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    if (StringUtils.isNotEmpty(criteria.getFromAddress())) {
        properties.add(new MatchesProperty("fromAddress", criteria.getFromAddress()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    if (StringUtils.isNotEmpty(criteria.getMessage())) {
        properties.add(new MatchesProperty("message", criteria.getMessage()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    if (StringUtils.isNotEmpty(criteria.getSubject())) {
        properties.add(new MatchesProperty("subject", criteria.getSubject()));
        extendQueryWithOrClause(queryBuilder, initialLength);
    }
    closeQuery(queryBuilder, initialLength);
    if (restriction != null && !restriction.isEmpty()) {
        properties.add(new RestrictionProperty(restriction, SecurityUtil.getUsername()));
        queryBuilder.append(" && %s");
    }
    QueryUtil.useFilterFromPattern(query, queryBuilder.toString(), properties);
    return execute(query, properties);
}
Also used : ArrayList(java.util.ArrayList) MatchesProperty(org.motechproject.mds.query.MatchesProperty) DateTime(org.joda.time.DateTime) RestrictionProperty(org.motechproject.mds.query.RestrictionProperty) DeliveryStatus(org.motechproject.email.domain.DeliveryStatus) SetProperty(org.motechproject.mds.query.SetProperty) Property(org.motechproject.mds.query.Property) MatchesProperty(org.motechproject.mds.query.MatchesProperty) RestrictionProperty(org.motechproject.mds.query.RestrictionProperty) RangeProperty(org.motechproject.mds.query.RangeProperty)

Aggregations

DeliveryStatus (org.motechproject.email.domain.DeliveryStatus)6 DateTime (org.joda.time.DateTime)5 EmailRecord (org.motechproject.email.domain.EmailRecord)5 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 EmailRecordSearchCriteria (org.motechproject.email.builder.EmailRecordSearchCriteria)2 HashSet (java.util.HashSet)1 MatchesProperty (org.motechproject.mds.query.MatchesProperty)1 Property (org.motechproject.mds.query.Property)1 RangeProperty (org.motechproject.mds.query.RangeProperty)1 RestrictionProperty (org.motechproject.mds.query.RestrictionProperty)1 SetProperty (org.motechproject.mds.query.SetProperty)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 CsvBeanWriter (org.supercsv.io.CsvBeanWriter)1