use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.
the class UntypedRecordableEventDescriptorComponentBuilderTest method setUp.
@Before
public void setUp() throws Exception {
queryManager = mocker.registerMockComponent(QueryManager.class);
modelBridge = mocker.registerMockComponent(ModelBridge.class);
documentReferenceResolver = mocker.registerMockComponent(DocumentReferenceResolver.class);
authorizationManager = mocker.registerMockComponent(AuthorizationManager.class);
Query query = mock(Query.class);
when(queryManager.createQuery(any(), any())).thenReturn(query);
when(query.execute()).thenReturn(Arrays.asList("e1", "e2", "e3"));
event1 = mock(DocumentReference.class);
event2 = mock(DocumentReference.class);
event3 = mock(DocumentReference.class);
when(this.documentReferenceResolver.resolve("e1")).thenReturn(event1);
when(this.documentReferenceResolver.resolve("e2")).thenReturn(event2);
when(this.documentReferenceResolver.resolve("e3")).thenReturn(event3);
}
use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.
the class QueryGeneratorTest method setUp.
@Before
public void setUp() throws Exception {
queryManager = mocker.getInstance(QueryManager.class);
notificationPreferenceManager = mocker.getInstance(NotificationPreferenceManager.class);
serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
userPreferencesSource = mocker.getInstance(ConfigurationSource.class, "user");
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
notificationFilterManager = mocker.getInstance(NotificationFilterManager.class);
startDate = new Date(10);
query = mock(Query.class);
when(queryManager.createQuery(anyString(), anyString())).thenReturn(query);
pref1StartDate = new Date(100000000);
NotificationPreference pref1 = mock(NotificationPreference.class);
when(pref1.getProperties()).thenReturn(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, "create"));
when(pref1.getFormat()).thenReturn(NotificationFormat.ALERT);
when(pref1.getStartDate()).thenReturn(pref1StartDate);
when(pref1.isNotificationEnabled()).thenReturn(true);
when(notificationPreferenceManager.getPreferences(userReference, true, NotificationFormat.ALERT)).thenReturn(Arrays.asList(pref1));
NotificationFilterPreference fakeFilterPreference = mock(NotificationFilterPreference.class);
when(fakeFilterPreference.isActive()).thenReturn(true);
when(notificationFilterManager.getFilterPreferences(any(DocumentReference.class))).thenReturn(Sets.newSet(fakeFilterPreference));
when(userPreferencesSource.getProperty("displayHiddenDocuments", 0)).thenReturn(0);
when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
}
use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.
the class StatsUtil method findVisitByField.
/**
* Search visit statistics object in the database based on cookie name.
*
* @param fieldName the field name.
* @param fieldValue the field value.
* @param context the XWiki context.
* @return the visit object, null if no object was found.
* @throws XWikiException error when searching for visit object.
* @since 1.4M1
*/
protected static VisitStats findVisitByField(String fieldName, String fieldValue, XWikiContext context) throws XWikiException {
VisitStats visitStats = null;
Date currentDate = new Date(new Date().getTime() - 30 * 60 * 1000);
QueryManager qm = context.getWiki().getStore().getQueryManager();
List<VisitStats> solist = null;
final String sfieldValue = "fieldValue";
final String sdate = "date";
if (qm.hasLanguage(Query.XPATH)) {
try {
solist = qm.createQuery("//element(*, xwiki:object)[@:{fieldName}=:{fieldValue}" + " and @endDate>:{date}] order by @endDate descending", Query.XPATH).bindValue("fieldName", fieldName).bindValue(sfieldValue, fieldValue).bindValue(sdate, currentDate).execute();
} catch (Exception e) {
LOGGER.error("Failed to search visit object in the jcr store from cookie name", e);
}
} else if (qm.hasLanguage(Query.HQL)) {
try {
solist = qm.createQuery("from VisitStats as obj " + "where obj." + fieldName + "=:fieldValue and obj.endDate > :date" + " order by obj.endDate desc", Query.HQL).bindValue(sfieldValue, fieldValue).bindValue(sdate, currentDate).execute();
} catch (Exception e) {
LOGGER.error("Failed to search visit object in the database from " + fieldName, e);
}
} else {
throw new UnsupportedOperationException("The current storage engine does not support querying statistics");
}
if (solist != null && solist.size() > 0) {
visitStats = solist.get(0);
}
return visitStats;
}
use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.
the class ExportActionTest method exportFullSpaceUsingWildcardsAsXAR.
@Test
public void exportFullSpaceUsingWildcardsAsXAR() throws Exception {
ExportAction action = new ExportAction();
XWikiContext context = oldcore.getXWikiContext();
// Make it a XAR export
XWikiRequest request = mock(XWikiRequest.class);
when(request.get("format")).thenReturn("xar");
context.setRequest(request);
// Set other request parameters
when(request.get("name")).thenReturn("myexport");
// Export all pages in the "Space" space
when(request.getParameterValues("pages")).thenReturn(new String[] { "Space.%" });
// Make the current user have programming rights
when(oldcore.getMockRightService().hasWikiAdminRights(context)).thenReturn(true);
// Query Manager-related mocking
QueryManager queryManager = oldcore.getMocker().registerMockComponent(QueryManager.class);
Query query = mock(Query.class);
when(queryManager.createQuery(anyString(), eq(Query.HQL))).thenReturn(query);
when(query.setWiki("xwiki")).thenReturn(query);
when(query.bindValues(any(List.class))).thenReturn(query);
when(query.execute()).thenReturn(Arrays.asList("Space.Page1", "Space.Page2"));
// Register some mock resolver to resolve passed page references
when(oldcore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", "xwiki:Space.Page1", context)).thenReturn(true);
when(oldcore.getMockRightService().hasAccessLevel("view", "XWiki.XWikiGuest", "xwiki:Space.Page2", context)).thenReturn(true);
DocumentReferenceResolver<String> resolver = oldcore.getMocker().registerMockComponent(DocumentReferenceResolver.TYPE_STRING, "current");
when(resolver.resolve("xwiki:Space.Page1")).thenReturn(new DocumentReference("xwiki", "Space", "Page1"));
when(resolver.resolve("xwiki:Space.Page2")).thenReturn(new DocumentReference("xwiki", "Space", "Page2"));
// Register some mock filters so that the export does nothing.
InputFilterStreamFactory inputFilterStreamFactory = oldcore.getMocker().registerMockComponent(InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
when(inputFilterStreamFactory.createInputFilterStream(anyMap())).thenReturn(mock(InputFilterStream.class));
BeanOutputFilterStreamFactory beanOutputFilterStreamFactory = mock(BeanOutputFilterStreamFactory.class);
oldcore.getMocker().registerComponent(OutputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize(), beanOutputFilterStreamFactory);
when(beanOutputFilterStreamFactory.createOutputFilterStream(any(XAROutputProperties.class))).thenReturn(mock(BeanOutputFilterStream.class));
// Set response stream
XWikiResponse response = mock(XWikiResponse.class);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
context.setResponse(response);
String result = action.render(oldcore.getXWikiContext());
// The tests are below this line!
// Verify null is returned (this means the response has been returned)
assertNull(result);
// Verify that the parameters passed to the input stream factory are defining the correct pages
ArgumentCaptor<DocumentInstanceInputProperties> properties = ArgumentCaptor.forClass(DocumentInstanceInputProperties.class);
verify(inputFilterStreamFactory).createInputFilterStream(properties.capture());
assertEquals(false, properties.getValue().isVerbose());
assertEquals(false, properties.getValue().isWithJRCSRevisions());
assertEquals(false, properties.getValue().isWithRevisions());
assertEquals(true, properties.getValue().getEntities().matches(new DocumentReference("xwiki", "Space", "Page1")));
assertEquals(true, properties.getValue().getEntities().matches(new DocumentReference("xwiki", "Space", "Page2")));
}
use of org.xwiki.query.QueryManager in project xwiki-platform by xwiki.
the class X509KeyWikiStoreTest method setUp.
@Before
public void setUp() throws Exception {
EntityReferenceProvider valueProvider = mock(EntityReferenceProvider.class);
when(valueProvider.getDefaultReference(EntityType.WIKI)).thenReturn(WIKI_REFERENCE);
when(valueProvider.getDefaultReference(EntityType.SPACE)).thenReturn(SPACE_REFERENCE);
when(valueProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(DOCUMENT_REFERENCE);
mocker.registerComponent(EntityReferenceProvider.class, "current", valueProvider);
Provider<XWikiContext> xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
xcontext = mock(XWikiContext.class);
when(xcontextProvider.get()).thenReturn(xcontext);
xwiki = mock(com.xpn.xwiki.XWiki.class);
when(xcontext.getWiki()).thenReturn(xwiki);
BinaryStringEncoder encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64");
when(encoder.encode(PRIVATEKEY, 64)).thenReturn(ENCODED_PRIVATEKEY);
when(encoder.decode(ENCODED_PRIVATEKEY)).thenReturn(PRIVATEKEY);
when(encoder.encode(ENCRYPTED_PRIVATEKEY, 64)).thenReturn(ENCODED_ENCRYPTED_PRIVATEKEY);
when(encoder.decode(ENCODED_ENCRYPTED_PRIVATEKEY)).thenReturn(ENCRYPTED_PRIVATEKEY);
when(encoder.encode(CERTIFICATE, 64)).thenReturn(ENCODED_CERTIFICATE);
when(encoder.decode(ENCODED_CERTIFICATE)).thenReturn(CERTIFICATE);
when(encoder.encode(SUBJECT_KEYID)).thenReturn(ENCODED_SUBJECTKEYID);
when(encoder.decode(ENCODED_SUBJECTKEYID)).thenReturn(SUBJECT_KEYID);
privateKey = mock(PrivateKeyParameters.class);
when(privateKey.getEncoded()).thenReturn(PRIVATEKEY);
AsymmetricKeyFactory keyFactory = mocker.getInstance(AsymmetricKeyFactory.class);
when(keyFactory.fromPKCS8(PRIVATEKEY)).thenReturn(privateKey);
PrivateKeyPasswordBasedEncryptor encryptor = mocker.getInstance(PrivateKeyPasswordBasedEncryptor.class);
when(encryptor.encrypt(PASSWORD, privateKey)).thenReturn(ENCRYPTED_PRIVATEKEY);
when(encryptor.decrypt(PASSWORD, ENCRYPTED_PRIVATEKEY)).thenReturn(privateKey);
certificate = mock(X509CertifiedPublicKey.class);
when(certificate.getSerialNumber()).thenReturn(SERIAL);
when(certificate.getIssuer()).thenReturn(new DistinguishedName(ISSUER));
when(certificate.getSubject()).thenReturn(new DistinguishedName(SUBJECT));
when(certificate.getEncoded()).thenReturn(CERTIFICATE);
CertificateFactory certificateFactory = mocker.getInstance(CertificateFactory.class, "X509");
when(certificateFactory.decode(CERTIFICATE)).thenReturn(certificate);
X509Extensions extensions = mock(X509Extensions.class);
when(certificate.getExtensions()).thenReturn(extensions);
when(extensions.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
when(certificate.getSubjectKeyIdentifier()).thenReturn(SUBJECT_KEYID);
keyPair = new CertifiedKeyPair(privateKey, certificate);
QueryManager queryManager = mocker.getInstance(QueryManager.class);
query = mock(Query.class);
when(query.bindValue(any(String.class), any())).thenReturn(query);
when(query.setWiki(WIKI)).thenReturn(query);
when(queryManager.createQuery(any(String.class), any(String.class))).thenReturn(query);
store = mocker.getComponentUnderTest();
}
Aggregations