Search in sources :

Example 1 with Range

use of org.motechproject.commons.api.Range 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 2 with Range

use of org.motechproject.commons.api.Range in project motech by motech.

the class PropertyBuilderTest method shouldGenerateAppropriatePropertyType.

@Test
public void shouldGenerateAppropriatePropertyType() throws Exception {
    Set<String> set = new HashSet<>();
    Range<Integer> range = new Range<>(0, 1);
    Type type = new Type("mds.field.combobox", "", List.class);
    Entity entity = new Entity("org.motechproject.mds.Sample");
    Field field = new Field(entity, "roles", "roles", type, true, false, false);
    // should not create collection property for fields without list field
    assertProperty(PropertyBuilder.create("roles", 1L, Long.class.getName()), EqualProperty.class, "roles", 1L);
    field.addSetting(new FieldSetting(field, new TypeSetting(Constants.Settings.ALLOW_MULTIPLE_SELECTIONS), "true"));
    // should create collection property for enum list
    assertProperty(PropertyBuilder.create(field, "role"), CollectionProperty.class, "roles", Arrays.asList("role"));
    field.addSetting(new FieldSetting(field, new TypeSetting(Constants.Settings.ALLOW_USER_SUPPLIED), "true"));
    // should create collection property for string list
    assertProperty(PropertyBuilder.create(field, "role"), CollectionProperty.class, "roles", Arrays.asList("role"));
    assertProperty(PropertyBuilder.create("set", set, String.class), SetProperty.class, "set", set);
    assertProperty(PropertyBuilder.create("range", range, Integer.class), RangeProperty.class, "range", range);
    assertProperty(PropertyBuilder.create("equal", 1L, Long.class), EqualProperty.class, "equal", 1L);
    assertProperty(PropertyBuilder.create("text", "someString", String.class.getName(), "matches()"), MatchesProperty.class, "text", ".*someString.*");
    assertProperty(PropertyBuilder.create("text", "someString", String.class.getName(), "matches((?i))"), MatchesCaseInsensitiveProperty.class, "text", "(?i).*someString.*");
}
Also used : FieldSetting(org.motechproject.mds.domain.FieldSetting) Entity(org.motechproject.mds.domain.Entity) Field(org.motechproject.mds.domain.Field) Type(org.motechproject.mds.domain.Type) TypeSetting(org.motechproject.mds.domain.TypeSetting) Range(org.motechproject.commons.api.Range) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Range

use of org.motechproject.commons.api.Range in project motech by motech.

the class QueryUtilTest method shouldCreateFiltersAndParamDeclarationForRanges.

@Test
public void shouldCreateFiltersAndParamDeclarationForRanges() {
    DateTime now = DateTime.now();
    DateTime later = now.plusHours(2);
    Range<DateTime> range = new Range<>(now, later);
    String[] properties = new String[] { "prop1", "prop2" };
    Object[] values = new Object[] { range, true };
    QueryUtil.useFilter(query, properties, values, typeMap(DateTime.class, Boolean.class));
    verify(query).setFilter("prop1>=param0lb && prop1<=param0ub && prop2 == param1");
    verify(query).declareParameters("org.joda.time.DateTime param0lb, org.joda.time.DateTime param0ub, java.lang.Boolean param1");
}
Also used : Range(org.motechproject.commons.api.Range) DateTime(org.joda.time.DateTime) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with Range

use of org.motechproject.commons.api.Range in project motech by motech.

the class TypeHelperTest method shouldBuildRanges.

@Test
public void shouldBuildRanges() {
    final DateTime now = DateUtil.now();
    final Range dtRange = new Range<>(now, now.plusHours(1));
    final Range intRange = new Range<>(1, 5);
    assertEquals(intRange, TypeHelper.toRange(new Range<>(1, 5), Integer.class.getName()));
    assertEquals(dtRange, TypeHelper.toRange(new Range<>(now, now.plusHours(1)), DateTime.class.getName()));
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("min", now);
    map.put("max", now.plusHours(1));
    assertEquals(dtRange, TypeHelper.toRange(map, DateTime.class.getName()));
    map.put("min", now.toString());
    map.put("max", now.plusHours(1).toString());
    assertEquals(dtRange, TypeHelper.toRange(map, DateTime.class.getName()));
    // string parsing
    assertEquals(dtRange, TypeHelper.toRange(dtRange.getMin() + ".." + dtRange.getMax(), DateTime.class.getName()));
    assertEquals(intRange, TypeHelper.toRange("1..5", Integer.class.getName()));
}
Also used : Range(org.motechproject.commons.api.Range) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with Range

use of org.motechproject.commons.api.Range in project motech by motech.

the class PasswordRecoveryServiceImpl method cleanUpExpiredRecoveries.

@Override
@Transactional
public void cleanUpExpiredRecoveries() {
    Range<DateTime> range = new Range<>(new DateTime(0), DateUtil.now());
    List<PasswordRecovery> expiredRecoveries = passwordRecoveriesDataService.findByExpirationDate(range);
    for (PasswordRecovery recovery : expiredRecoveries) {
        passwordRecoveriesDataService.delete(recovery);
    }
    LOGGER.info("Cleaned up all expired password recoveries");
}
Also used : Range(org.motechproject.commons.api.Range) PasswordRecovery(org.motechproject.security.domain.PasswordRecovery) DateTime(org.joda.time.DateTime) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Range (org.motechproject.commons.api.Range)8 DateTime (org.joda.time.DateTime)4 Test (org.junit.Test)4 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 ZonedDateTime (java.time.ZonedDateTime)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 BidiMap (org.apache.commons.collections.BidiMap)1 DualHashBidiMap (org.apache.commons.collections.bidimap.DualHashBidiMap)1 UnmodifiableBidiMap (org.apache.commons.collections.bidimap.UnmodifiableBidiMap)1 EmailRecordSearchCriteria (org.motechproject.email.builder.EmailRecordSearchCriteria)1