AbstractSearchExecution implements QueryExecution<T> { @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); } AbstractSearchExecution(EmailRecordSearchCriteria criteria); @Override T execute(Query query, InstanceSecurityRestriction restriction);  }
@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); }