/**
 * 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 junit.framework.TestCase;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;


public class AccessFilterTest extends TestCase {

	private Directory dir;
	private IndexReader reader;
	private IndexSearcher searcher;
	private Query allDocs;


	protected void setUp() throws Exception {
		dir = new RAMDirectory();
		Analyzer analyzer = new StandardAnalyzer();

		Document[] docs = {
			getDocument("public1", "0"),
			getDocument("public2", "0"),

			getDocument("protected1_group1", "1"),
			getDocument("protected2_group1,2", "1,2")
		};

		IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
		conf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

		IndexWriter writer = new IndexWriter(dir, conf);

		for (Document doc : docs) {
			writer.addDocument(doc);
		}
		writer.close();

		reader = DirectoryReader.open(dir);
		searcher = new IndexSearcher(reader);
		allDocs = new MatchAllDocsQuery();
	}

	protected Document getDocument(String title, String access) {
		Document doc = new Document();

		doc.add(new StringField("title", title, Field.Store.YES));
		doc.add(new StringField("access", access, Field.Store.YES));

		return doc;
	}

	protected void tearDown() throws Exception {
		reader.close();
		dir.close();
	}

	public void testAccessFilterFindsOnlyPublicDocumentsWhenNotLoggedIn() throws Exception {
		Filter filter = new AccessFilter();

		TopDocs hits = searcher.search(allDocs, filter, 10);
		assertEquals("only public documents", 2, hits.totalHits);
	}

	public void testAccessFilterFindsDocumentsAllowedForGroup1() throws Exception {
		Filter filter = new AccessFilter("0,1");

		TopDocs hits = searcher.search(allDocs, filter, 10);
		assertEquals("public documents and for group 1", 3, hits.totalHits);
		assertEquals("allows access for document 'protected1_group1'",
			"protected1_group1",
			searcher.doc(hits.scoreDocs[2].doc).get("title")
		);
	}

	public void testAccessFilterFindsDocumentsRequiringAccessForGroup1And2() throws Exception {
		Filter filter = new AccessFilter("0,1,2");

		TopDocs hits = searcher.search(allDocs, filter, 10);
		assertEquals("public documents and for group 1 and group 2", 4, hits.totalHits);
	}

	public void testAccessFilterFiltersDocumentsWithInsufficientAccess() throws Exception {
		Filter filter = new AccessFilter("0,2");

		TopDocs hits = searcher.search(allDocs, filter, 10);
		assertEquals("public documents", 2, hits.totalHits);
	}

}

@@ -18,17 +18,17 @@
 2010-201
-1
+5
  Ingo Re
@@ -2821,16 +2821,24 @@
 Equals(%22
+returns 
 only pub
@@ -2865,32 +2865,32 @@
 its.totalHits);%0A
-
 %09%7D%0A%0A%09public void
@@ -3065,32 +3065,40 @@
 %09%09assertEquals(%22
+returns 
 public documents
@@ -3488,32 +3488,40 @@
 %09%09assertEquals(%22
+returns 
 public documents
@@ -3779,22 +3779,34 @@
 Equals(%22
-public
+returns accessible
  documen

Tweak error messages for test faiures