Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.workbench.common.services.refactoring.backend.server.query.findrules;
  
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import  javax.enterprise.inject.Instance;
 
 import org.junit.Test;
 import  org.kie.workbench.common.services.refactoring.model.index.terms.ProjectRootPathIndexTerm;
 import  org.kie.workbench.common.services.refactoring.model.index.terms.RuleAttributeIndexTerm;
 import  org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueIndexTerm;
 import  org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValuePackageNameIndexTerm;
 import  org.kie.workbench.common.services.refactoring.model.index.terms.valueterms.ValueProjectRootPathIndexTerm;
 import  org.kie.workbench.common.services.refactoring.model.query.RefactoringPageRequest;
 import  org.kie.workbench.common.services.refactoring.model.query.RefactoringPageRow;
 import  org.uberfire.ext.metadata.backend.lucene.analyzer.FilenameAnalyzer;
 import  org.uberfire.java.nio.file.Path;
 import  org.uberfire.paging.PageResponse;
 
 import static org.apache.lucene.util.Version.*;
 import static org.junit.Assert.*;
 import static org.mockito.Mockito.*;
 
 
     private Set<NamedQueryqueries = new HashSet<NamedQuery>() {{
         addnew FindRulesByProjectQuery() );
     }};
 
     @Test
     public void testQueryValidIndexTerms() throws IOExceptionInterruptedException {
         final Instance<NamedQuerynamedQueriesProducer = mock( Instance.class );
         when( namedQueriesProducer.iterator() ).thenReturn( .iterator() );
 
         final RefactoringQueryServiceImpl service = new RefactoringQueryServiceImplgetConfig(),
                                                                                      namedQueriesProducer );
         service.init();
 
         //Add test files
         final Path path1 = .resolve( "drl1.drl" );
         final String drl1 = loadText"drl1.drl" );
         ioService().write( path1,
                            drl1 );
         final Path path2 = .resolve( "drl2.drl" );
         final String drl2 = loadText"drl2.drl" );
         ioService().write( path2,
                            drl2 );
         final Path path3 = .resolve( "drl3.drl" );
         final String drl3 = loadText"drl3.drl" );
         ioService().write( path3,
                            drl3 );
 
         Thread.sleep( 5000 ); //wait for events to be consumed from jgit -> (notify changes -> watcher -> index) -> lucene index
 
         {
             final RefactoringPageRequest request = new RefactoringPageRequest( "FindRulesByProjectQuery",
                                                                                new HashSet<ValueIndexTerm>() {{
                                                                                    addnew ValueProjectRootPathIndexTerm( "*" ) );
                                                                                    addnew ValuePackageNameIndexTerm( "*" ) );
                                                                                }},
                                                                                true,
                                                                                0,
                                                                                10 );
 
             try {
                 final PageResponse<RefactoringPageRow> response = service.queryrequest );
                 assertNotNullresponse );
                 assertEquals( 3,
                               response.getPageRowList().size() );
                 assertResponseContainsresponse.getPageRowList(),
                                         "myRule" );
                 assertResponseContainsresponse.getPageRowList(),
                                         "myRule2" );
                 assertResponseContainsresponse.getPageRowList(),
                                         "myRule3" );
 
             } catch ( IllegalArgumentException e ) {
                 fail();
             }
         }
 
         {
             final RefactoringPageRequest request = new RefactoringPageRequest( "FindRulesByProjectQuery",
                                                                                new HashSet<ValueIndexTerm>() {{
                                                                                    addnew ValueProjectRootPathIndexTerm( . ) );
                                                                                    addnew ValuePackageNameIndexTerm( . ) );
                                                                                }},
                                                                               false,
                                                                               0,
                                                                               10 );
            try {
                final PageResponse<RefactoringPageRow> response = service.queryrequest );
                assertNotNullresponse );
                assertEquals( 3,
                              response.getPageRowList().size() );
                assertResponseContainsresponse.getPageRowList(),
                                        "myRule" );
                assertResponseContainsresponse.getPageRowList(),
                                        "myRule2" );
                assertResponseContainsresponse.getPageRowList(),
                                        "myRule3" );
            } catch ( IllegalArgumentException e ) {
                fail();
            }
        }
        {
            final RefactoringPageRequest request = new RefactoringPageRequest( "FindRulesByProjectQuery",
                                                                               new HashSet<ValueIndexTerm>() {{
                                                                                   addnew ValueProjectRootPathIndexTerm( . ) );
                                                                                   addnew ValuePackageNameIndexTerm( "non-existent-package-name" ) );
                                                                               }},
                                                                               false,
                                                                               0,
                                                                               10 );
            try {
                final PageResponse<RefactoringPageRow> response = service.queryrequest );
                assertNotNullresponse );
                assertEquals( 0,
                              response.getPageRowList().size() );
            } catch ( IllegalArgumentException e ) {
                fail();
            }
        }
        {
            final RefactoringPageRequest request = new RefactoringPageRequest( "FindRulesByProjectQuery",
                                                                               new HashSet<ValueIndexTerm>() {{
                                                                                   addnew ValueProjectRootPathIndexTerm( "non-existent-project-root" ) );
                                                                                   addnew ValuePackageNameIndexTerm( . ) );
                                                                               }},
                                                                               false,
                                                                               0,
                                                                               10 );
            try {
                final PageResponse<RefactoringPageRow> response = service.queryrequest );
                assertNotNullresponse );
                assertEquals( 0,
                              response.getPageRowList().size() );
            } catch ( IllegalArgumentException e ) {
                fail();
            }
        }
    }
    @Override
    protected TestIndexer getIndexer() {
        return new TestDrlFileIndexer();
    }
    @Override
    public Map<StringAnalyzergetAnalyzers() {
        return new HashMap<StringAnalyzer>() {{
            put( RuleAttributeIndexTerm.TERM,
                 new RuleAttributeNameAnalyzer ) );
            put( ProjectRootPathIndexTerm.TERM,
                 new FilenameAnalyzer(  ) );
        }};
    }
    @Override
        return new TestDrlFileTypeDefinition();
    }
    @Override
    protected String getRepositoryName() {
        return this.getClass().getSimpleName();
    }
    private void assertResponseContainsfinal List<RefactoringPageRow> rows,
                                         final String ruleName ) {
        for ( RefactoringPageRow row : rows ) {
            final String rowRuleName = ( (Stringrow.getValue() );
            if ( rowRuleName.equalsruleName ) ) {
                return;
            }
        }
        fail"Response does not contain expected Rule Name '" + ruleName + "'." );
    }
New to GrepCode? Check out our FAQ X