Search in sources :

Example 1 with DefaultQuery

use of org.xwiki.query.internal.DefaultQuery in project xwiki-platform by xwiki.

the class HqlQueryExecutorTest method createNamedNativeHibernateQuery.

@Test
@SuppressWarnings("unchecked")
public void createNamedNativeHibernateQuery() throws Exception {
    DefaultQuery query = new DefaultQuery("queryName", this.executor);
    Session session = mock(Session.class);
    SQLQuery sqlQuery = mock(SQLQuery.class);
    when(session.getNamedQuery(query.getStatement())).thenReturn(sqlQuery);
    when(sqlQuery.getQueryString()).thenReturn("foo");
    // Add a Query Filter to verify it's called and can change the statement
    QueryFilter filter = mock(QueryFilter.class);
    query.addFilter(filter);
    when(filter.filterStatement("foo", "sql")).thenReturn("bar");
    when(filter.filterQuery(any(Query.class))).then(returnsFirstArg());
    SQLQuery finalQuery = mock(SQLQuery.class);
    when(session.createSQLQuery("bar")).thenReturn(finalQuery);
    NamedSQLQueryDefinition definition = mock(NamedSQLQueryDefinition.class);
    when(definition.getResultSetRef()).thenReturn("someResultSet");
    HibernateSessionFactory sessionFactory = this.mocker.getInstance(HibernateSessionFactory.class);
    sessionFactory.getConfiguration().getNamedSQLQueries().put(query.getStatement(), definition);
    assertSame(finalQuery, this.executor.createHibernateQuery(session, query));
    verify(finalQuery).setResultSetMapping(definition.getResultSetRef());
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) NamedSQLQueryDefinition(org.hibernate.engine.NamedSQLQueryDefinition) 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) HibernateSessionFactory(com.xpn.xwiki.store.hibernate.HibernateSessionFactory) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with DefaultQuery

use of org.xwiki.query.internal.DefaultQuery 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 3 with DefaultQuery

use of org.xwiki.query.internal.DefaultQuery 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 4 with DefaultQuery

use of org.xwiki.query.internal.DefaultQuery in project xwiki-platform by xwiki.

the class SolrQueryExecutorTest method filterResponse.

@Test
public void filterResponse() throws Exception {
    ParameterizedType resolverType = new DefaultParameterizedType(null, DocumentReferenceResolver.class, SolrDocument.class);
    DocumentReferenceResolver<SolrDocument> resolver = this.componentManager.getInstance(resolverType);
    AuthorizationManager authorizationManager = this.componentManager.getInstance(AuthorizationManager.class);
    DocumentReference currentUserReference = new DocumentReference("xwiki", "XWiki", "currentuser");
    this.oldCore.getXWikiContext().setUserReference(currentUserReference);
    DocumentReference currentAuthorReference = new DocumentReference("xwiki", "XWiki", "currentauthor");
    XWikiDocument currentDocument = new XWikiDocument(currentAuthorReference);
    currentDocument.setContentAuthorReference(currentAuthorReference);
    this.oldCore.getXWikiContext().setDoc(currentDocument);
    DocumentReference aliceReference = new DocumentReference("wiki", "Users", "Alice");
    when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, aliceReference)).thenReturn(true);
    SolrDocument alice = new SolrDocument();
    when(resolver.resolve(alice)).thenReturn(aliceReference);
    DocumentReference bobReference = new DocumentReference("wiki", "Users", "Bob");
    when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, bobReference)).thenReturn(true);
    when(authorizationManager.hasAccess(Right.VIEW, currentAuthorReference, bobReference)).thenReturn(true);
    SolrDocument bob = new SolrDocument();
    when(resolver.resolve(bob)).thenReturn(bobReference);
    DocumentReference carolReference = new DocumentReference("wiki", "Users", "Carol");
    when(authorizationManager.hasAccess(Right.VIEW, currentUserReference, carolReference)).thenReturn(true);
    SolrDocument carol = new SolrDocument();
    when(resolver.resolve(carol)).thenReturn(carolReference);
    SolrDocumentList sourceResults = new SolrDocumentList();
    sourceResults.addAll(Arrays.asList(alice, bob, carol));
    sourceResults.setNumFound(3);
    QueryResponse response = mock(QueryResponse.class);
    when(this.solr.query(any(SolrParams.class))).thenReturn(response);
    DefaultQuery query = new DefaultQuery("", null);
    // No right check
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    SolrDocumentList results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(alice, bob, carol), results);
    // Check current user right
    query.checkCurrentUser(true);
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(bob, carol), results);
    // Check both current user and author rights
    query.checkCurrentAuthor(true);
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(bob), results);
    // Check current author right
    query.checkCurrentUser(false);
    when(response.getResults()).thenReturn((SolrDocumentList) sourceResults.clone());
    results = ((QueryResponse) this.componentManager.getComponentUnderTest().execute(query).get(0)).getResults();
    assertEquals(Arrays.asList(alice, bob), results);
}
Also used : DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) SolrDocument(org.apache.solr.common.SolrDocument) DefaultQuery(org.xwiki.query.internal.DefaultQuery) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrParams(org.apache.solr.common.params.SolrParams) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 5 with DefaultQuery

use of org.xwiki.query.internal.DefaultQuery in project celements-blog by celements.

the class ArticleNullDatesMigratorTest method testMigrate.

@Test
public void testMigrate() throws Exception {
    Query query = new DefaultQuery("", queryExecutorMock);
    expect(queryManagerMock.createQuery(eq(migrator.getXWQL()), eq("xwql"))).andReturn(query).once();
    expect(queryExecutorMock.execute(same(query))).andReturn(Arrays.<Object>asList("space.article1", "space.article2")).once();
    List<DocumentReference> docRefs = Arrays.asList(new DocumentReference("xwikidb", "space", "article1"), new DocumentReference("xwikidb", "space", "article2"));
    XWikiDocument doc1 = new XWikiDocument(docRefs.get(0));
    expect(xwiki.getDocument(eq(docRefs.get(0)), same(context))).andReturn(doc1).once();
    xwiki.saveDocument(same(doc1), eq("article null dates migration"), same(context));
    expectLastCall().once();
    XWikiDocument doc2 = new XWikiDocument(docRefs.get(1));
    expect(xwiki.getDocument(eq(docRefs.get(1)), same(context))).andReturn(doc2).once();
    xwiki.saveDocument(same(doc2), eq("article null dates migration"), same(context));
    expectLastCall().once();
    replayDefault();
    migrator.migrate(null, context);
    verifyDefault();
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DefaultQuery(org.xwiki.query.internal.DefaultQuery) Query(org.xwiki.query.Query) DefaultQuery(org.xwiki.query.internal.DefaultQuery) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

DefaultQuery (org.xwiki.query.internal.DefaultQuery)8 Test (org.junit.Test)6 Query (org.xwiki.query.Query)4 SQLQuery (org.hibernate.SQLQuery)3 Session (org.hibernate.Session)3 QueryFilter (org.xwiki.query.QueryFilter)3 WrappingQuery (org.xwiki.query.WrappingQuery)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 HibernateSessionFactory (com.xpn.xwiki.store.hibernate.HibernateSessionFactory)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrParams (org.apache.solr.common.params.SolrParams)1 NamedSQLQueryDefinition (org.hibernate.engine.NamedSQLQueryDefinition)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1