Search in sources :

Example 1 with CodeResult

use of com.searchcode.app.dto.CodeResult in project searchcode-server by boyter.

the class CodeMatcher method formatResults.

/**
     * Entry point for matching lines
     */
public List<CodeResult> formatResults(List<CodeResult> codeResult, String matchTerms, boolean highlightLine) {
    List<String> lstMatchTerms = splitTerms(matchTerms);
    List<CodeResult> results = new ArrayList<>();
    for (CodeResult code : codeResult) {
        List<CodeMatchResult> result = matchResults(code.getCode(), lstMatchTerms, highlightLine);
        if (result != null) {
            code.setMatchingResults(result);
            results.add(code);
        }
    }
    return results;
}
Also used : CodeResult(com.searchcode.app.dto.CodeResult) CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Example 2 with CodeResult

use of com.searchcode.app.dto.CodeResult in project searchcode-server by boyter.

the class EndToEndITCase method testEndToEndSvnPath.

public void testEndToEndSvnPath() throws IOException {
    CodeSearcher cs = new CodeSearcher();
    File directoryWithFiles = TestHelpers.createDirectoryWithFiles("EndToEndSvnTest");
    IndexSvnRepoJob indexSvnRepoJob = new IndexSvnRepoJob();
    indexSvnRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), false);
    SearchResult searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    CodeResult codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    CodeResult codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    CodeResult codeResult3 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult3.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete a single file
    String codeId = searchResult.getCodeResultList().get(0).getCodeId();
    Singleton.getCodeIndexer().deleteByCodeId(codeId);
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    // Delete file from disk then index to ensure it is removed from the index
    File toDelete = new File(directoryWithFiles.toString() + "/EndToEndTestFile2.py");
    toDelete.delete();
    indexSvnRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), true);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    Singleton.getCodeIndexer().deleteByReponame("ENDTOENDTEST");
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(0);
}
Also used : IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileUtils(org.apache.commons.io.FileUtils) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob) TestHelpers(com.searchcode.app.TestHelpers) Values(com.searchcode.app.config.Values) CodeIndexer(com.searchcode.app.service.CodeIndexer) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) java.io(java.io) Paths(java.nio.file.Paths) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) Properties(com.searchcode.app.util.Properties) TestCase(junit.framework.TestCase) IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) Path(java.nio.file.Path) CodeResult(com.searchcode.app.dto.CodeResult) IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 3 with CodeResult

use of com.searchcode.app.dto.CodeResult in project searchcode-server by boyter.

the class EndToEndITCase method testEndToEndFilePath.

public void testEndToEndFilePath() throws IOException {
    CodeSearcher cs = new CodeSearcher();
    File directoryWithFiles = TestHelpers.createDirectoryWithFiles("EndToEndFileTest");
    IndexFileRepoJob indexFileRepoJob = new IndexFileRepoJob();
    // Index created files
    indexFileRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), false);
    SearchResult searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(3);
    CodeResult codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    CodeResult codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    CodeResult codeResult3 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult3.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete a single file
    String codeId = searchResult.getCodeResultList().get(0).getCodeId();
    Singleton.getCodeIndexer().deleteByCodeId(codeId);
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile2.py")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile2");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete file from disk then index to ensure it is removed from the index
    File toDelete = new File(directoryWithFiles.toString() + "/EndToEndTestFile2.py");
    toDelete.delete();
    indexFileRepoJob.indexDocsByPath(Paths.get(directoryWithFiles.toString()), "ENDTOENDTEST", "", directoryWithFiles.toString(), true);
    searchResult = cs.search("endtoendtestfile", 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(2);
    codeResult1 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile1.php")).findFirst().get();
    codeResult2 = searchResult.getCodeResultList().stream().filter(x -> x.getFileName().equals("EndToEndTestFile3.java")).findFirst().get();
    assertThat(codeResult1.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile1");
    assertThat(codeResult2.getCode().get(0)).isEqualTo("EndToEndTestFile EndToEndTestFile3");
    // Delete everything
    Singleton.getCodeIndexer().deleteByReponame("ENDTOENDTEST");
    searchResult = cs.search("endtoendtestfile".toLowerCase(), 0);
    assertThat(searchResult.getCodeResultList().size()).isEqualTo(0);
}
Also used : IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) IndexSvnRepoJob(com.searchcode.app.jobs.repository.IndexSvnRepoJob) SearchResult(com.searchcode.app.dto.SearchResult) Singleton(com.searchcode.app.service.Singleton) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileUtils(org.apache.commons.io.FileUtils) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob) TestHelpers(com.searchcode.app.TestHelpers) Values(com.searchcode.app.config.Values) CodeIndexer(com.searchcode.app.service.CodeIndexer) CodeResult(com.searchcode.app.dto.CodeResult) CodeSearcher(com.searchcode.app.service.CodeSearcher) java.io(java.io) Paths(java.nio.file.Paths) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) Properties(com.searchcode.app.util.Properties) TestCase(junit.framework.TestCase) IndexFileRepoJob(com.searchcode.app.jobs.repository.IndexFileRepoJob) Path(java.nio.file.Path) CodeResult(com.searchcode.app.dto.CodeResult) SearchResult(com.searchcode.app.dto.SearchResult) CodeSearcher(com.searchcode.app.service.CodeSearcher)

Example 4 with CodeResult

use of com.searchcode.app.dto.CodeResult in project searchcode-server by boyter.

the class CodeMatcherTest method testFormatResults.

public void testFormatResults() {
    CodeMatcher cm = new CodeMatcher();
    List<CodeResult> codeResults = new ArrayList<>();
    List<String> code = new ArrayList<>();
    code.add("this is some test code");
    code.add("List<String> something = *p;");
    code.add("List<String> test = *p;");
    code.add("this does not match at all");
    codeResults.add(new CodeResult(code, null));
    cm.formatResults(codeResults, "List<String> test = *p;", true);
}
Also used : ArrayList(java.util.ArrayList) CodeResult(com.searchcode.app.dto.CodeResult)

Example 5 with CodeResult

use of com.searchcode.app.dto.CodeResult in project searchcode-server by boyter.

the class CodeRouteServiceTest method testGetCodeWithParamsWithMatch.

public void testGetCodeWithParamsWithMatch() {
    Request request = Mockito.mock(Request.class);
    Response response = Mockito.mock(Response.class);
    CodeSearcher codeSearcher = Mockito.mock(CodeSearcher.class);
    CodeRouteService codeRouteService = new CodeRouteService(codeSearcher);
    CodeResult codeResult = new CodeResult(new ArrayList<String>(), new ArrayList<CodeMatchResult>());
    codeResult.setCodeLines("100");
    codeResult.setLanguageName("LanguageName");
    codeResult.setMd5hash("md5hash");
    codeResult.setRepoName("myRepo");
    codeResult.setRepoLocation("repoLocation");
    codeResult.setCodeOwner("codeOwner");
    when(request.params(":codeid")).thenReturn("MATCH-MOCK");
    when(codeSearcher.getByCodeId("MATCH-MOCK")).thenReturn(codeResult);
    Map<String, Object> map = codeRouteService.getCode(request, response);
    assertThat(map.get("codePath")).isEqualTo("/");
    assertThat(map.get("codeLength")).isEqualTo("100");
    assertThat(map.get("languageName")).isEqualTo("LanguageName");
    assertThat(map.get("md5Hash")).isEqualTo("md5hash");
    assertThat(map.get("repoName")).isEqualTo("myRepo");
    assertThat(map.get("highlight")).isEqualTo(true);
    assertThat(map.get("repoLocation")).isEqualTo("repoLocation");
    assertThat(map.get("codeValue")).isEqualTo("");
    assertThat(map.get("highligher")).isNotNull();
    assertThat(map.get("codeOwner")).isEqualTo("codeOwner");
    assertThat(map.get("owaspResults")).isNotNull();
    assertThat(map.get("logoImage")).isNotNull();
    assertThat(map.get("isCommunity")).isEqualTo(App.ISCOMMUNITY);
    assertThat(map.get("estimatedCost")).isNull();
}
Also used : Response(spark.Response) CodeRouteService(com.searchcode.app.service.route.CodeRouteService) Request(spark.Request) CodeResult(com.searchcode.app.dto.CodeResult) CodeMatchResult(com.searchcode.app.dto.CodeMatchResult)

Aggregations

CodeResult (com.searchcode.app.dto.CodeResult)8 SearchResult (com.searchcode.app.dto.SearchResult)5 Values (com.searchcode.app.config.Values)4 CodeSearcher (com.searchcode.app.service.CodeSearcher)4 Singleton (com.searchcode.app.service.Singleton)4 TestHelpers (com.searchcode.app.TestHelpers)3 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)3 IndexFileRepoJob (com.searchcode.app.jobs.repository.IndexFileRepoJob)3 IndexGitRepoJob (com.searchcode.app.jobs.repository.IndexGitRepoJob)3 IndexSvnRepoJob (com.searchcode.app.jobs.repository.IndexSvnRepoJob)3 CodeIndexer (com.searchcode.app.service.CodeIndexer)3 Properties (com.searchcode.app.util.Properties)3 java.io (java.io)3 Path (java.nio.file.Path)3 Paths (java.nio.file.Paths)3 TestCase (junit.framework.TestCase)3 FileUtils (org.apache.commons.io.FileUtils)3 AssertionsForInterfaceTypes.assertThat (org.assertj.core.api.AssertionsForInterfaceTypes.assertThat)3 CodeMatchResult (com.searchcode.app.dto.CodeMatchResult)2 CodeMatcher (com.searchcode.app.service.CodeMatcher)2