<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.search.solr.script;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;

import javax.inject.Provider;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.SpaceReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.search.solr.script.SolrIndexScriptService;
import org.xwiki.security.authorization.AuthorizationManager;
import org.xwiki.security.authorization.Right;
import org.xwiki.test.mockito.MockitoComponentMockingRule;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;

/**
 * Tests for the {@link org.xwiki.search.solr.script.SolrIndexScriptService}.
 * 
 * @version $Id$
 */
public class SolrIndexScriptServiceTest
{
    @Rule
    public final MockitoComponentMockingRule<SolrIndexScriptService> mocker =
        new MockitoComponentMockingRule<SolrIndexScriptService>(SolrIndexScriptService.class);

    private XWikiContext mockContext;

    private XWiki mockXWiki;

    private XWikiDocument mockCurrentDocument;

    private DocumentReference userReference;

    private DocumentReference contentAuthorReference;

    private SolrIndexScriptService service;

    private Logger logger;

    private AuthorizationManager mockAuthorization;

    @Before
    public void setUp() throws Exception
    {
        this.service = mocker.getComponentUnderTest();

        this.logger = mocker.getMockedLogger();

        this.userReference = new DocumentReference("wiki", "space", "user");

        // Context
        this.mockContext = mock(XWikiContext.class);
        Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
        when(xcontextProvider.get()).thenReturn(this.mockContext);

        // XWiki
        this.mockXWiki = mock(XWiki.class);
        when(mockContext.getWiki()).thenReturn(this.mockXWiki);

        this.mockCurrentDocument = mock(XWikiDocument.class);
        when(mockContext.getDoc()).thenReturn(this.mockCurrentDocument);

        when(mockContext.getWikiId()).thenReturn("currentWiki");
        when(mockContext.getUserReference()).thenReturn(userReference);

        // RightService
        this.mockAuthorization = this.mocker.getInstance(AuthorizationManager.class);
        // By default, we have the rights.
        when(mockAuthorization.hasAccess(any(Right.class), any(DocumentReference.class), any(EntityReference.class)))
            .thenReturn(true);

        // Rights check success. By default we are allowed (no error is thrown)
        verify(this.logger, never()).error(anyString(), any(IllegalAccessException.class));
    }

    @Test
    public void indexSingleReferenceChecksRights() throws Exception
    {
        EntityReference wikiReference = new WikiReference("someWiki");

        // Call
        this.service.index(wikiReference);

        // Assert and verify

        // Actual rights check
        verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
        verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
    }

    @Test
    public void indexMultipleReferencesChecksRights() throws Exception
    {
        EntityReference wikiReference = new WikiReference("someWiki");<commit_msg>[misc] Fix test<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.search.solr.script;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;

import javax.inject.Provider;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReference;
import org.xwiki.model.reference.SpaceReference;
import org.xwiki.model.reference.WikiReference;
import org.xwiki.security.authorization.AuthorizationManager;
import org.xwiki.security.authorization.Right;
import org.xwiki.test.mockito.MockitoComponentMockingRule;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.doc.XWikiDocument;

/**
 * Tests for the {@link org.xwiki.search.solr.script.SolrIndexScriptService}.
 * 
 * @version $Id$
 */
public class SolrIndexScriptServiceTest
{
    @Rule
    public final MockitoComponentMockingRule<SolrIndexScriptService> mocker =
        new MockitoComponentMockingRule<SolrIndexScriptService>(SolrIndexScriptService.class);

    private XWikiContext mockContext;

    private XWiki mockXWiki;

    private XWikiDocument mockCurrentDocument;

    private DocumentReference userReference;

    private DocumentReference contentAuthorReference;

    private SolrIndexScriptService service;

    private Logger logger;

    private AuthorizationManager mockAuthorization;

    @Before
    public void setUp() throws Exception
    {
        this.userReference = new DocumentReference("wiki", "space", "user");

        // Context
        this.mockContext = mock(XWikiContext.class);
        Provider<XWikiContext> xcontextProvider = this.mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER);
        when(xcontextProvider.get()).thenReturn(this.mockContext);

        // XWiki
        this.mockXWiki = mock(XWiki.class);
        when(mockContext.getWiki()).thenReturn(this.mockXWiki);

        this.mockCurrentDocument = mock(XWikiDocument.class);
        when(mockContext.getDoc()).thenReturn(this.mockCurrentDocument);

        when(mockContext.getWikiId()).thenReturn("currentWiki");
        when(mockContext.getUserReference()).thenReturn(userReference);

        // RightService
        this.mockAuthorization = this.mocker.getInstance(AuthorizationManager.class);
        // By default, we have the rights.
        when(mockAuthorization.hasAccess(any(Right.class), any(DocumentReference.class), any(EntityReference.class)))
            .thenReturn(true);

        this.service = mocker.getComponentUnderTest();

        // Rights check success. By default we are allowed (no error is thrown)
        this.logger = mocker.getMockedLogger();
        verify(this.logger, never()).error(anyString(), any(IllegalAccessException.class));
    }

    @Test
    public void indexSingleReferenceChecksRights() throws Exception
    {
        EntityReference wikiReference = new WikiReference("someWiki");

        // Call
        this.service.index(wikiReference);

        // Assert and verify

        // Actual rights check
        verify(this.mockAuthorization).hasAccess(Right.ADMIN, this.userReference, wikiReference);
        verify(this.mockAuthorization).hasAccess(Right.PROGRAM, this.contentAuthorReference, wikiReference);
    }

    @Test
    public void indexMultipleReferencesChecksRights() throws Exception
    {
        EntityReference wikiReference = new WikiReference("someWiki");
{'fraction_non_alphanumeric': 0.09054991991457555, 'fraction_numerical': 0.002989855846235985, 'mean_word_length': 4.021983914209115, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '383426', 'n_tokens_mistral': 2614, 'n_tokens_neox': 2535, 'n_words': 663}