/**
 * Copyright 2010-2011 Ingo Renner <ingo@typo3.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.typo3.solr.search;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.params.CommonParams;
import org.junit.BeforeClass;


/**
 * Test class to test the Query Parser Plugin
 *
 * @author Ingo Renner <ingo@typo3.org>
 */
public class AccessFilterQParserTest extends SolrTestCaseJ4 {

	// setUp and tearDown

	@BeforeClass
	public static void beforeClass() throws Exception {
		initCore("solrconfig.xml", "schema.xml");
		createIndex();
	}

	private static void createIndex() {
		assertU(addDocument("public1", "0"));
		assertU(addDocument("public2", "0"));
		assertU(addDocument("protected1_group1", "1"));
		assertU(addDocument("protected2_group1,2", "1,2"));

		assertU(commit());
	}


	// tests


	public void testSolrFindsOnlyPublicDocumentsWhenNotLoggedIn() {
		assertQ(
			"couldn't find public documents",
			req(
					"q", "*:*",
					"qt", "/select",
					"fq", "{!typo3access}0",
					CommonParams.VERSION, "2.2"
				),
			"//result[@numFound=2]",
			"//str[@name='id'][.='public1']",
			"//str[@name='id'][.='public2']"
		);
	}

	public void testSolrFindsDocumentsAllowedForGroup1() throws Exception {
		assertQ(
			"couldn't find public documents and documents for group 1",
			req(
					"q", "*:*",
					"qt", "/select",
					"fq", "{!typo3access}0,1",
					CommonParams.VERSION, "2.2"
				),
			"//result[@numFound=3]",
			"//str[@name='id'][.='public1']",
			"//str[@name='id'][.='public2']",
			"//str[@name='id'][.='protected1_group1']"
		);
	}

	public void testSolrFindsDocumentsRequiringAccessForGroup1And2() throws Exception {
		assertQ(
			"couldn't find public documents and documents for group 1 and group 2",
			req(
					"q", "*:*",
					"qt", "/select",
					"fq", "{!typo3access}0,1,2",
					CommonParams.VERSION, "2.2"
				),
			"//result[@numFound=4]"
		);
	}

	public void testSolrFiltersDocumentsWithInsufficientAccess() throws Exception {
		assertQ(
			"found public documents only",
			req(
					"q", "*:*",
					"qt", "/select",
					"fq", "{!typo3access}0,2",
					CommonParams.VERSION, "2.2"
				),
			"//result[@numFound=2]",
			"//str[@name='id'][.='public1']",
			"//str[@name='id'][.='public2']"
		);
	}


	// utilities


	private static String addDocument(String title, String access) {
		return adoc(
			"id", title,
			"title", title,
			"access", access
		);
	}

}

@@ -698,45 +698,24 @@
 org.
-apache.solr.common.params.CommonParam
+junit.BeforeClas
 s;%0Ai
@@ -730,27 +730,20 @@
 g.junit.
-BeforeClass
+Test
 ;%0A%0A%0A/**%0A
@@ -1313,17 +1313,23 @@
  tests%0A%0A
+%09@Test
 %0A
-
 %09public 
@@ -1512,42 +1512,8 @@
 s%7D0%22
-,%0A%09%09%09%09%09CommonParams.VERSION, %222.2%22
 %0A%09%09%09
@@ -1618,32 +1618,39 @@
 ic2'%5D%22%0A%09%09);%0A%09%7D%0A%0A
+%09@Test%0A
 %09public void tes
@@ -1861,42 +1861,8 @@
 0,1%22
-,%0A%09%09%09%09%09CommonParams.VERSION, %222.2%22
 %0A%09%09%09
@@ -2014,32 +2014,39 @@
 up1'%5D%22%0A%09%09);%0A%09%7D%0A%0A
+%09@Test%0A
 %09public void tes
@@ -2283,42 +2283,8 @@
 1,2%22
-,%0A%09%09%09%09%09CommonParams.VERSION, %222.2%22
 %0A%09%09%09
@@ -2323,16 +2323,23 @@
 %09);%0A%09%7D%0A%0A
+%09@Test%0A
 %09public 
@@ -2495,32 +2495,32 @@
 qt%22, %22/select%22,%0A
+
 %09%09%09%09%09%22fq%22, %22%7B!ty
@@ -2537,42 +2537,8 @@
 0,2%22
-,%0A%09%09%09%09%09CommonParams.VERSION, %222.2%22
 %0A%09%09%09

Clean up AccessFilterQParserTest