Search in sources :

Example 1 with QueryManager

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);
}
Also used : DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) Query(org.xwiki.query.Query) QueryManager(org.xwiki.query.QueryManager) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 2 with QueryManager

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");
}
Also used : NotificationFilterManager(org.xwiki.notifications.filters.NotificationFilterManager) NotificationPreference(org.xwiki.notifications.preferences.NotificationPreference) ConfigurationSource(org.xwiki.configuration.ConfigurationSource) NotificationPreferenceManager(org.xwiki.notifications.preferences.NotificationPreferenceManager) Query(org.xwiki.query.Query) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) QueryManager(org.xwiki.query.QueryManager) NotificationFilterPreference(org.xwiki.notifications.filters.NotificationFilterPreference) Date(java.util.Date) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 3 with QueryManager

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;
}
Also used : QueryManager(org.xwiki.query.QueryManager) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException) MalformedURLException(java.net.MalformedURLException)

Example 4 with QueryManager

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")));
}
Also used : BeanOutputFilterStreamFactory(org.xwiki.filter.output.BeanOutputFilterStreamFactory) BeanOutputFilterStream(org.xwiki.filter.output.BeanOutputFilterStream) Query(org.xwiki.query.Query) ServletOutputStream(javax.servlet.ServletOutputStream) XWikiContext(com.xpn.xwiki.XWikiContext) InputFilterStreamFactory(org.xwiki.filter.input.InputFilterStreamFactory) InputFilterStream(org.xwiki.filter.input.InputFilterStream) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) QueryManager(org.xwiki.query.QueryManager) List(java.util.List) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 5 with QueryManager

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();
}
Also used : BinaryStringEncoder(org.xwiki.crypto.BinaryStringEncoder) Query(org.xwiki.query.Query) DistinguishedName(org.xwiki.crypto.pkix.params.x509certificate.DistinguishedName) PrivateKeyParameters(org.xwiki.crypto.params.cipher.asymmetric.PrivateKeyParameters) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) X509Extensions(org.xwiki.crypto.pkix.params.x509certificate.extension.X509Extensions) CertificateFactory(org.xwiki.crypto.pkix.CertificateFactory) AsymmetricKeyFactory(org.xwiki.crypto.AsymmetricKeyFactory) EntityReferenceProvider(org.xwiki.model.reference.EntityReferenceProvider) X509CertifiedPublicKey(org.xwiki.crypto.pkix.params.x509certificate.X509CertifiedPublicKey) QueryManager(org.xwiki.query.QueryManager) PrivateKeyPasswordBasedEncryptor(org.xwiki.crypto.password.PrivateKeyPasswordBasedEncryptor) CertifiedKeyPair(org.xwiki.crypto.pkix.params.CertifiedKeyPair) Before(org.junit.Before)

Aggregations

QueryManager (org.xwiki.query.QueryManager)22 Query (org.xwiki.query.Query)18 DocumentReference (org.xwiki.model.reference.DocumentReference)11 Before (org.junit.Before)7 Test (org.junit.Test)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWiki (com.xpn.xwiki.XWiki)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 XWikiException (com.xpn.xwiki.XWikiException)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 Map (java.util.Map)4 BaseObject (com.xpn.xwiki.objects.BaseObject)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 QueryException (org.xwiki.query.QueryException)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 Expectations (org.jmock.Expectations)2 ConfigurationSource (org.xwiki.configuration.ConfigurationSource)2