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());
}
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'");
}
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));
}
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);
}
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();
}
Aggregations