Search in sources :

Example 1 with EmailRecordSearchCriteria

use of org.motechproject.email.builder.EmailRecordSearchCriteria in project motech by motech.

the class EmailAuditServiceBundleIT method shouldCountAndRetrieveEmailsBasedOnCriteria.

@Test
public void shouldCountAndRetrieveEmailsBasedOnCriteria() {
    emailRecordService.create(createEmailRecord("address@1.com", "something", DeliveryStatus.SENT));
    emailRecordService.create(createEmailRecord("address@2.com", "a subject", DeliveryStatus.SENT));
    emailRecordService.create(createEmailRecord("address3@1.com", "received this?", DeliveryStatus.RECEIVED));
    emailRecordService.create(createEmailRecord("something@1.com", "was sent", DeliveryStatus.SENT));
    emailRecordService.create(createEmailRecord("address@1.com", "error", DeliveryStatus.ERROR));
    QueryParams queryParams = new QueryParams(1, 10, new Order("subject", Order.Direction.DESC));
    EmailRecordSearchCriteria criteria = new EmailRecordSearchCriteria().withToAddress("address").withDeliveryStatuses(DeliveryStatus.SENT, DeliveryStatus.RECEIVED).withQueryParams(queryParams).withMessageTimeRange(new Range<DateTime>(null, null));
    assertEquals(3, emailAuditService.countEmailRecords(criteria));
    List<EmailRecord> records = emailAuditService.findEmailRecords(criteria);
    assertEquals(asList("something", "received this?", "a subject"), extract(records, on(EmailRecord.class).getSubject()));
    assertEquals(asList("address@1.com", "address3@1.com", "address@2.com"), extract(records, on(EmailRecord.class).getToAddress()));
    assertEquals(asList(DeliveryStatus.SENT, DeliveryStatus.RECEIVED, DeliveryStatus.SENT), extract(records, on(EmailRecord.class).getDeliveryStatus()));
}
Also used : Order(org.motechproject.mds.util.Order) EmailRecord(org.motechproject.email.domain.EmailRecord) QueryParams(org.motechproject.mds.query.QueryParams) EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with EmailRecordSearchCriteria

use of org.motechproject.email.builder.EmailRecordSearchCriteria 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 3 with EmailRecordSearchCriteria

use of org.motechproject.email.builder.EmailRecordSearchCriteria in project motech by motech.

the class AbstractSearchExecutionTest method shouldSetCorrectParams.

@Test
public void shouldSetCorrectParams() {
    when(restriction.isEmpty()).thenReturn(false);
    when(restriction.isByCreator()).thenReturn(true);
    when(query.executeWithArray(anyVararg())).thenReturn(queryResult());
    EmailRecordSearchCriteria criteria = new EmailRecordSearchCriteria().withFromAddress("from@address.com").withMessageTimeRange(new Range<>(PAST, FUTURE)).withMessage("text to search").withDeliveryStatuses(DeliveryStatus.RECEIVED, DeliveryStatus.ERROR).withQueryParams(queryParams);
    AbstractSearchExecution execution = createExecution(criteria);
    Object result = execution.execute(query, restriction);
    verify(query).setFilter("deliveryTime>=param0lb && deliveryTime<=param0ub && " + "(deliveryStatus == param1_0 || deliveryStatus == param1_1) && (fromAddress.matches(param2) || " + "message.matches(param3)) && creator == param4");
    verify(query).declareParameters("org.joda.time.DateTime param0lb, org.joda.time.DateTime param0ub, " + DeliveryStatus.class.getName() + " param1_0, " + DeliveryStatus.class.getName() + " param1_1, " + "java.lang.String param2, java.lang.String param3, java.lang.String param4");
    verify(query).executeWithArray(argThat(new QueryExecutionMatcher()));
    verifyImplementationDetails();
    assertResult(result);
}
Also used : EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) Range(org.motechproject.commons.api.Range) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with EmailRecordSearchCriteria

use of org.motechproject.email.builder.EmailRecordSearchCriteria in project motech by motech.

the class EmailControllerTest method shouldExportEmailMonthAsCsv.

@Test
public void shouldExportEmailMonthAsCsv() throws Exception {
    StringWriter writer = new StringWriter();
    when(response.getWriter()).thenReturn(new PrintWriter(writer));
    emailController.exportEmailLog("month", "01-1969", response, request);
    ArgumentCaptor<EmailRecordSearchCriteria> captor = ArgumentCaptor.forClass(EmailRecordSearchCriteria.class);
    verify(auditService).findEmailRecords(captor.capture());
    assertTrue(captor.getValue().getDeliveryTimeRange().getMin().isEqual(DateTime.parse("1969-01-01T00:00:00.000")));
    assertTrue(captor.getValue().getDeliveryTimeRange().getMax().isEqual(DateTime.parse("1969-01-31T23:59:59.999")));
    assertEquals(writer.toString(), getTestEmailRecordsAsCsv());
}
Also used : StringWriter(java.io.StringWriter) EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 5 with EmailRecordSearchCriteria

use of org.motechproject.email.builder.EmailRecordSearchCriteria in project motech by motech.

the class EmailControllerTest method shouldExportEmailTableAsCsv.

@Test
public void shouldExportEmailTableAsCsv() throws Exception {
    StringWriter writer = new StringWriter();
    GridSettings filter = new GridSettings();
    filter.setDeliveryStatus("SENT");
    filter.setPage(1);
    filter.setRows(5);
    filter.setTimeFrom("1969-01-01 00:00:00");
    filter.setTimeTo("1969-01-31 23:59:59");
    when(lastFilter.get(anyString())).thenReturn(filter);
    when(response.getWriter()).thenReturn(new PrintWriter(writer));
    emailController.exportEmailLog("table", null, response, request);
    ArgumentCaptor<EmailRecordSearchCriteria> captor = ArgumentCaptor.forClass(EmailRecordSearchCriteria.class);
    verify(auditService).findEmailRecords(captor.capture());
    assertArrayEquals(new DeliveryStatus[] { DeliveryStatus.SENT }, captor.getValue().getDeliveryStatuses().toArray());
    assertEquals(filter.getPage(), captor.getValue().getQueryParams().getPage());
    assertEquals(filter.getRows(), captor.getValue().getQueryParams().getPageSize());
    assertTrue(captor.getValue().getDeliveryTimeRange().getMin().isEqual(DateTime.parse("1969-01-01T00:00:00")));
    assertTrue(captor.getValue().getDeliveryTimeRange().getMax().isEqual(DateTime.parse("1969-01-31T23:59:59")));
    assertEquals(getTestEmailRecordsAsCsv(), writer.toString());
}
Also used : StringWriter(java.io.StringWriter) EmailRecordSearchCriteria(org.motechproject.email.builder.EmailRecordSearchCriteria) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

EmailRecordSearchCriteria (org.motechproject.email.builder.EmailRecordSearchCriteria)8 Test (org.junit.Test)5 EmailRecord (org.motechproject.email.domain.EmailRecord)4 DateTime (org.joda.time.DateTime)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 DeliveryStatus (org.motechproject.email.domain.DeliveryStatus)2 QueryParams (org.motechproject.mds.query.QueryParams)2 Order (org.motechproject.mds.util.Order)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Range (org.motechproject.commons.api.Range)1 EmailRecords (org.motechproject.email.domain.EmailRecords)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 CsvBeanWriter (org.supercsv.io.CsvBeanWriter)1