@Test void filterExpressionWithSubQuery() throws Exception { NotificationPreference pref1 = mock(NotificationPreference.class); NotificationPreference pref2 = mock(NotificationPreference.class); Map<NotificationPreferenceProperty, Object> properties1 = new HashMap<>(); Map<NotificationPreferenceProperty, Object> properties2 = new HashMap<>(); when(pref1.getProperties()).thenReturn(properties1); when(pref2.getProperties()).thenReturn(properties2); properties1.put(NotificationPreferenceProperty.EVENT_TYPE, "type1"); properties2.put(NotificationPreferenceProperty.EVENT_TYPE, "type2"); when(pref1.isNotificationEnabled()).thenReturn(true); when(pref2.isNotificationEnabled()).thenReturn(true); when(pref1.getStartDate()).thenReturn(new Date(0)); when(pref2.getStartDate()).thenReturn(new Date(100000L)); List<NotificationPreference> notificationFilterPreferences = Arrays.asList(pref1, pref2); DocumentReference pageRef = new DocumentReference("wikiA", "SpaceB", "PageC"); NotificationFilterPreference prefγ = mockNotificationFilterPreference("wikiA:SpaceB:PageC", pageRef, NotificationFilterType.INCLUSIVE, null); NotificationFilterPreference prefz = mockNotificationFilterPreference("wikiA:SpaceB:PageD", pageRef, NotificationFilterType.EXCLUSIVE, null); Collection<NotificationFilterPreference> filterPreferences = Sets.newSet(prefγ); DocumentReference user = new DocumentReference("xwiki", "XWiki", "User"); assertEquals( "(((TYPE = \"type1\" AND DATE >= \"" + new Date(0).toString() + "\") " + "OR (TYPE = \"type2\" AND DATE >= \"" + new Date(100000L).toString() + "\")) " + "AND CONCAT(CONCAT(WIKI, \":\"), PAGE) IN " + "(SELECT nfp.pageOnly FROM DefaultNotificationFilterPreference nfp WHERE nfp.owner = :owner " + "AND nfp.filterType = 0 AND nfp.filterName = 'scopeNotificationFilter' " + "AND nfp.pageOnly IS NOT NULL AND nfp.pageOnly <> '' " + "AND (nfp.allEventTypes = '' OR nfp.allEventTypes IS NULL) " + "AND nfp.alertEnabled = true AND nfp.enabled = true AND nfp.startingDate <= event.date))", this.scopeNotificationFilter.filterExpression(user, filterPreferences, NotificationFilterType.INCLUSIVE, NotificationFormat.ALERT, notificationFilterPreferences).toString()); assertNull(this.scopeNotificationFilter.filterExpression(user, filterPreferences, NotificationFilterType.EXCLUSIVE, NotificationFormat.ALERT, notificationFilterPreferences)); filterPreferences = Sets.newSet(prefγ, prefz); assertEquals( "NOT (CONCAT(CONCAT(WIKI, \":\"), PAGE) IN (SELECT nfp.pageOnly " + "FROM DefaultNotificationFilterPreference nfp " + "WHERE nfp.owner = :owner AND nfp.filterType = 1 AND nfp.filterName = 'scopeNotificationFilter' " + "AND nfp.pageOnly IS NOT NULL AND nfp.pageOnly <> '' " + "AND (nfp.allEventTypes = '' OR nfp.allEventTypes IS NULL) " + "AND nfp.emailEnabled = true AND nfp.enabled = true))", this.scopeNotificationFilter.filterExpression(user, filterPreferences, NotificationFilterType.EXCLUSIVE, NotificationFormat.EMAIL, notificationFilterPreferences).toString()); }
@Override public ExpressionNode filterExpression(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences, NotificationPreference preference) { return expressionGenerator.filterExpression(filterPreferences, (String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE), preference.getFormat(), user); }
ScopeNotificationFilter implements NotificationFilter { @Override public ExpressionNode filterExpression(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences, NotificationPreference preference) { return expressionGenerator.filterExpression(filterPreferences, (String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE), preference.getFormat(), user); } }
ScopeNotificationFilter implements NotificationFilter { @Override public ExpressionNode filterExpression(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences, NotificationPreference preference) { return expressionGenerator.filterExpression(filterPreferences, (String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE), preference.getFormat(), user); }  }
ScopeNotificationFilter implements NotificationFilter { @Override public ExpressionNode filterExpression(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences, NotificationPreference preference) { return expressionGenerator.filterExpression(filterPreferences, (String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE), preference.getFormat(), user); }  @Override FilterPolicy filterEvent(Event event, DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences,
            NotificationFormat format); @Override boolean matchesPreference(NotificationPreference preference); @Override ExpressionNode filterExpression(DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences,
            NotificationPreference preference); @Override ExpressionNode filterExpression(DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences,
            NotificationFilterType type, NotificationFormat format); @Override ExpressionNode filterExpression(DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences, NotificationFilterType type,
            NotificationFormat format, Collection<NotificationPreference> preferences); @Override String getName(); }
ScopeNotificationFilter implements NotificationFilter { @Override public ExpressionNode filterExpression(DocumentReference user, Collection<NotificationFilterPreference> filterPreferences, NotificationPreference preference) { return expressionGenerator.filterExpression(filterPreferences, (String) preference.getProperties().get(NotificationPreferenceProperty.EVENT_TYPE), preference.getFormat(), user); }  @Override FilterPolicy filterEvent(Event event, DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences,
            NotificationFormat format); @Override boolean matchesPreference(NotificationPreference preference); @Override ExpressionNode filterExpression(DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences,
            NotificationPreference preference); @Override ExpressionNode filterExpression(DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences,
            NotificationFilterType type, NotificationFormat format); @Override ExpressionNode filterExpression(DocumentReference user,
            Collection<NotificationFilterPreference> filterPreferences, NotificationFilterType type,
            NotificationFormat format, Collection<NotificationPreference> preferences); @Override String getName(); static final String FILTER_NAME; }