Search in sources :

Example 6 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class HqlQueryExecutorTest method createHibernateQueryWhenFilter.

@Test
public void createHibernateQueryWhenFilter() throws Exception {
    Session session = mock(Session.class);
    DefaultQuery query = new DefaultQuery("where doc.space='Main'", Query.HQL, this.executor);
    // Add a Query Filter to verify it's called and can change the statement.
    // We also verify that QueryFilter#filterStatement() is called before QueryFilter#filterQuery()
    QueryFilter filter = mock(QueryFilter.class);
    query.addFilter(filter);
    when(filter.filterStatement("select doc.fullName from XWikiDocument doc where doc.space='Main'", Query.HQL)).thenReturn("select doc.fullName from XWikiDocument doc where doc.space='Main2'");
    when(filter.filterQuery(any(Query.class))).thenReturn(new WrappingQuery(query) {

        @Override
        public String getStatement() {
            return "select doc.fullName from XWikiDocument doc where doc.space='Main3'";
        }
    });
    this.executor.createHibernateQuery(session, query);
    // The test is here!
    verify(session).createQuery("select doc.fullName from XWikiDocument doc where doc.space='Main3'");
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) DefaultQuery(org.xwiki.query.internal.DefaultQuery) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) DefaultQuery(org.xwiki.query.internal.DefaultQuery) WrappingQuery(org.xwiki.query.WrappingQuery) WrappingQuery(org.xwiki.query.WrappingQuery) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class HqlQueryExecutorTest method createHibernateQueryAutomaticallyAddEscapeLikeParametersFilterWhenQueryParameter.

@Test
public void createHibernateQueryAutomaticallyAddEscapeLikeParametersFilterWhenQueryParameter() throws Exception {
    Session session = mock(Session.class);
    DefaultQuery query = new DefaultQuery("where space like :space", Query.HQL, this.executor);
    query.bindValue("space").literal("test");
    QueryFilter filter = mock(QueryFilter.class);
    when(filter.filterStatement(anyString(), anyString())).then(returnsFirstArg());
    when(filter.filterQuery(any(Query.class))).then(returnsFirstArg());
    ComponentManager cm = this.mocker.getInstance(ComponentManager.class, "context");
    when(cm.getInstance(QueryFilter.class, "escapeLikeParameters")).thenReturn(filter);
    when(session.createQuery(anyString())).thenReturn(mock(org.hibernate.Query.class));
    this.executor.createHibernateQuery(session, query);
    // The test is here! We verify that the filter has been called even though we didn't explicitly add it to the
    // query, i.e. that it's been added automatically
    verify(filter).filterQuery(any(Query.class));
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) DefaultQuery(org.xwiki.query.internal.DefaultQuery) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) DefaultQuery(org.xwiki.query.internal.DefaultQuery) WrappingQuery(org.xwiki.query.WrappingQuery) ComponentManager(org.xwiki.component.manager.ComponentManager) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class DatabaseDocumentIteratorTest method iterateOneWiki.

@Test
public void iterateOneWiki() throws Exception {
    DocumentReference rootReference = createDocumentReference("gang", Arrays.asList("A", "B"), "C", null);
    Query emptyQuery = mock(Query.class);
    when(emptyQuery.execute()).thenReturn(Collections.emptyList());
    Query query = mock(Query.class);
    when(query.setLimit(anyInt())).thenReturn(query);
    when(query.setWiki(rootReference.getWikiReference().getName())).thenReturn(query);
    when(query.setOffset(0)).thenReturn(query);
    when(query.setOffset(100)).thenReturn(emptyQuery);
    when(query.execute()).thenReturn(Collections.<Object>singletonList(new Object[] { "A.B", "C", "de", "3.1" }));
    Map<String, Object> namedParameters = new HashMap<String, Object>();
    namedParameters.put("space", "A.B");
    namedParameters.put("name", "C");
    when(query.getNamedParameters()).thenReturn(namedParameters);
    Query countQuery = mock(Query.class);
    when(countQuery.addFilter(mocker.<QueryFilter>getInstance(QueryFilter.class, "count"))).thenReturn(countQuery);
    QueryManager queryManager = mocker.getInstance(QueryManager.class);
    String whereClause = " where doc.space = :space and doc.name = :name";
    when(queryManager.createQuery("select doc.space, doc.name, doc.language, doc.version from XWikiDocument doc" + whereClause + " order by doc.space, doc.name, doc.language", Query.HQL)).thenReturn(query);
    when(queryManager.createQuery(whereClause, Query.HQL)).thenReturn(countQuery);
    DocumentIterator<String> iterator = mocker.getComponentUnderTest();
    iterator.setRootReference(rootReference);
    List<Pair<DocumentReference, String>> actualResults = new ArrayList<Pair<DocumentReference, String>>();
    while (iterator.hasNext()) {
        actualResults.add(iterator.next());
    }
    List<Pair<DocumentReference, String>> expectedResults = new ArrayList<Pair<DocumentReference, String>>();
    expectedResults.add(new ImmutablePair<DocumentReference, String>(new DocumentReference(rootReference, Locale.GERMAN), "3.1"));
    assertEquals(expectedResults, actualResults);
    verify(query).bindValue("space", "A.B");
    verify(query).bindValue("name", "C");
    verify(countQuery).bindValue("space", "A.B");
    verify(countQuery).bindValue("name", "C");
}
Also used : Query(org.xwiki.query.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryFilter(org.xwiki.query.QueryFilter) QueryManager(org.xwiki.query.QueryManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Test(org.junit.Test)

Example 9 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class XWikiDocumentMockitoTest method getChildrenReferences.

@Test
public void getChildrenReferences() throws Exception {
    Query query = mock(Query.class);
    when(this.oldcore.getQueryManager().createQuery(any(), eq(Query.XWQL))).thenReturn(query);
    QueryFilter hiddenFilter = this.oldcore.getMocker().registerMockComponent(QueryFilter.class, "hidden");
    when(query.setLimit(7)).thenReturn(query);
    List<String> result = Arrays.asList("X.y", "A.b");
    when(query.<String>execute()).thenReturn(result);
    List<DocumentReference> childrenReferences = document.getChildrenReferences(7, 3, this.oldcore.getXWikiContext());
    verify(query).addFilter(hiddenFilter);
    verify(query).setLimit(7);
    verify(query).setOffset(3);
    Assert.assertEquals(2, childrenReferences.size());
    Assert.assertEquals(new DocumentReference("wiki", "X", "y"), childrenReferences.get(0));
    Assert.assertEquals(new DocumentReference("wiki", "A", "b"), childrenReferences.get(1));
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 10 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class DatabaseDocumentIteratorTest method iterateAllWikis.

@Test
public void iterateAllWikis() throws Exception {
    Query emptyQuery = mock(Query.class);
    when(emptyQuery.execute()).thenReturn(Collections.emptyList());
    Query chessQuery = mock(Query.class);
    when(chessQuery.setOffset(0)).thenReturn(chessQuery);
    when(chessQuery.setOffset(100)).thenReturn(emptyQuery);
    when(chessQuery.execute()).thenReturn(Arrays.<Object>asList(new Object[] { "Blog.Code", "WebHome", "", "3.2" }, new Object[] { "Main", "Welcome", "en", "1.1" }, new Object[] { "XWiki.Syntax", "Links", "fr", "2.5" }));
    DocumentReference chessBlogCodeWebHome = createDocumentReference("chess", Arrays.asList("Blog", "Code"), "WebHome", null);
    DocumentReference chessMainWelcome = createDocumentReference("chess", Arrays.asList("Main"), "Welcome", Locale.ENGLISH);
    DocumentReference chessXWikiSyntaxLinks = createDocumentReference("chess", Arrays.asList("XWiki", "Syntax"), "Links", Locale.FRENCH);
    Query tennisQuery = mock(Query.class);
    when(tennisQuery.setOffset(0)).thenReturn(tennisQuery);
    when(tennisQuery.setOffset(100)).thenReturn(emptyQuery);
    when(tennisQuery.execute()).thenReturn(Arrays.<Object>asList(new Object[] { "Main", "Welcome", "en", "2.1" }, new Object[] { "XWiki.Syntax", "Links", "fr", "1.3" }));
    DocumentReference tennisMainWelcome = createDocumentReference("tennis", Arrays.asList("Main"), "Welcome", Locale.ENGLISH);
    DocumentReference tennisXWikiSyntaxLinks = createDocumentReference("tennis", Arrays.asList("XWiki", "Syntax"), "Links", Locale.FRENCH);
    Query query = mock(Query.class);
    when(query.setLimit(anyInt())).thenReturn(query);
    when(query.getNamedParameters()).thenReturn(Collections.<String, Object>emptyMap());
    when(query.setWiki("chess")).thenReturn(chessQuery);
    when(query.setWiki("tennis")).thenReturn(tennisQuery);
    Query chessCountQuery = mock(Query.class);
    when(chessCountQuery.execute()).thenReturn(Collections.<Object>singletonList(3L));
    Query tennisCountQuery = mock(Query.class);
    when(tennisCountQuery.execute()).thenReturn(Collections.<Object>singletonList(2L));
    Query countQuery = mock(Query.class);
    when(countQuery.addFilter(mocker.<QueryFilter>getInstance(QueryFilter.class, "count"))).thenReturn(countQuery);
    when(countQuery.setWiki("chess")).thenReturn(chessCountQuery);
    when(countQuery.setWiki("tennis")).thenReturn(tennisCountQuery);
    QueryManager queryManager = mocker.getInstance(QueryManager.class);
    when(queryManager.createQuery("select doc.space, doc.name, doc.language, doc.version from XWikiDocument doc" + " order by doc.space, doc.name, doc.language", Query.HQL)).thenReturn(query);
    when(queryManager.createQuery("", Query.HQL)).thenReturn(countQuery);
    DocumentIterator<String> iterator = mocker.getComponentUnderTest();
    assertEquals(5L, iterator.size());
    List<Pair<DocumentReference, String>> actualResults = new ArrayList<Pair<DocumentReference, String>>();
    while (iterator.hasNext()) {
        actualResults.add(iterator.next());
    }
    List<Pair<DocumentReference, String>> expectedResults = new ArrayList<Pair<DocumentReference, String>>();
    expectedResults.add(new ImmutablePair<DocumentReference, String>(chessBlogCodeWebHome, "3.2"));
    expectedResults.add(new ImmutablePair<DocumentReference, String>(chessMainWelcome, "1.1"));
    expectedResults.add(new ImmutablePair<DocumentReference, String>(chessXWikiSyntaxLinks, "2.5"));
    expectedResults.add(new ImmutablePair<DocumentReference, String>(tennisMainWelcome, "2.1"));
    expectedResults.add(new ImmutablePair<DocumentReference, String>(tennisXWikiSyntaxLinks, "1.3"));
    assertEquals(expectedResults, actualResults);
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) QueryManager(org.xwiki.query.QueryManager) ArrayList(java.util.ArrayList) DocumentReference(org.xwiki.model.reference.DocumentReference) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Test(org.junit.Test)

Aggregations

QueryFilter (org.xwiki.query.QueryFilter)20 Query (org.xwiki.query.Query)15 Test (org.junit.Test)8 QueryException (org.xwiki.query.QueryException)7 SQLQuery (org.hibernate.SQLQuery)5 WrappingQuery (org.xwiki.query.WrappingQuery)5 XWikiException (com.xpn.xwiki.XWikiException)4 Session (org.hibernate.Session)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 SecureQuery (org.xwiki.query.SecureQuery)4 QueryManager (org.xwiki.query.QueryManager)3 DefaultQuery (org.xwiki.query.internal.DefaultQuery)3 Document (com.xpn.xwiki.api.Document)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 XWikiRestException (org.xwiki.rest.XWikiRestException)2