/*
* Copyright 2014 JBoss, by Red Hat, Inc
*
* 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.drools.workbench.screens.dtablexls.backend.server.indexing;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.drools.workbench.screens.dtablexls.type.DecisionTableXLSResourceTypeDefinition;
import org.junit.Test;
import org.kie.workbench.common.services.refactoring.backend.server.BaseIndexingTest;
import org.kie.workbench.common.services.refactoring.backend.server.TestIndexer;
import org.kie.workbench.common.services.refactoring.backend.server.indexing.RuleAttributeNameAnalyzer;
import org.kie.workbench.common.services.refactoring.backend.server.query.QueryBuilder;
import org.kie.workbench.common.services.refactoring.model.index.terms.RuleAttributeIndexTerm;
import org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueRuleAttributeIndexTerm;
import org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueRuleAttributeValueIndexTerm;
import org.uberfire.java.nio.file.Path;
import org.uberfire.metadata.backend.lucene.index.LuceneIndex;
import org.uberfire.metadata.backend.lucene.util.KObjectUtil;
import org.uberfire.metadata.engine.Index;
import org.uberfire.metadata.model.KObject;
import static org.apache.lucene.util.Version.*;
import static org.junit.Assert.*;
public class IndexDecisionTableXLSAttributeNameAndValueCompositionTest extends BaseIndexingTest<DecisionTableXLSResourceTypeDefinition> {
@Test
public void testIndexDecisionTableXLSAttributeNameAndValueComposition() throws IOException, InterruptedException {
//Don't ask, but we need to write a single file first in order for indexing to work
final Path basePath = getDirectoryPath().resolveSibling( "someNewOtherPath" );
ioService().write( basePath.resolve( "dummy" ),
"<none>" );
//Add test files
final Path path1 = loadXLSFile( basePath,
"dtable3.xls" );
Thread.sleep( 5000 ); //wait for events to be consumed from jgit -> (notify changes -> watcher -> index) -> lucene index
final Index index = getConfig().getIndexManager().get( org.uberfire.metadata.io.KObjectUtil.toKCluster( basePath.getFileSystem() ) );
//Decision Table defining a RuleFlow-Group named myRuleFlowGroup. This should match dtable3.xls
//This checks whether there is a Rule Attribute "ruleflow-group" and its Value is "myRuleflowGroup"
{
final IndexSearcher searcher = ( (LuceneIndex) index ).nrtSearcher();
final TopScoreDocCollector collector = TopScoreDocCollector.create( 10,
true );
final Query query = new QueryBuilder().addTerm( new ValueRuleAttributeIndexTerm( "ruleflow-group" ) ).addTerm( new ValueRuleAttributeValueIndexTerm( "myRuleFlowGroup" ) ).build();
searcher.search( query,
collector );
final ScoreDoc[] hits = collector.topDocs().scoreDocs;
assertEquals( 1,
hits.length );
final List<KObject> results = new ArrayList<KObject>();
for ( int i = 0; i < hits.length; i++ ) {
results.add( KObjectUtil.toKObject( searcher.doc( hits[ i ].doc ) ) );
}
assertContains( results,
path1 );
( (LuceneIndex) index ).nrtRelease( searcher );
}
//Decision Table defining a RuleFlow-Group named myAgendaGroup. This should *NOT* match dtable3.xls
{
final IndexSearcher searcher = ( (LuceneIndex) index ).nrtSearcher();
final TopScoreDocCollector collector = TopScoreDocCollector.create( 10,
true );
final Query query = new QueryBuilder().addTerm( new ValueRuleAttributeIndexTerm( "ruleflow-group" ) ).addTerm( new ValueRuleAttributeValueIndexTerm( "myAgendaGroup" ) ).build();
searcher.search( query,
collector );
final ScoreDoc[] hits = collector.topDocs().scoreDocs;
assertEquals( 0,
hits.length );
( (LuceneIndex) index ).nrtRelease( searcher );
}
}
@Override
protected TestIndexer getIndexer() {
return new TestDecisionTableXLSFileIndexer();
}
@Override
public Map<String, Analyzer> getAnalyzers() {
return new HashMap<String, Analyzer>() {{
put( RuleAttributeIndexTerm.TERM,
new RuleAttributeNameAnalyzer( LUCENE_40 ) );
}};
}
@Override
protected DecisionTableXLSResourceTypeDefinition getResourceTypeDefinition() {
return new DecisionTableXLSResourceTypeDefinition();
}
@Override
protected String getRepositoryName() {
return this.getClass().getSimpleName();
}
private Path loadXLSFile( final Path basePath,
final String fileName ) throws IOException {
final Path path = basePath.resolve( fileName );
final InputStream is = this.getClass().getResourceAsStream( fileName );
final OutputStream os = ioService().newOutputStream( path );
IOUtils.copy( is,
os );
os.flush();
os.close();
return path;
}
}