Search in sources :

Example 11 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class DataServiceTest method testTestAddToPersistent.

public void testTestAddToPersistent() {
    Data mockData = Mockito.mock(Data.class);
    DataService dataService = new DataService(mockData);
    when(mockData.getDataByName(Values.PERSISTENT_DELETE_QUEUE, "[]")).thenReturn("[]");
    dataService.addToPersistentDelete("test");
    verify(mockData, times(1)).saveData(any(), any());
}
Also used : Data(com.searchcode.app.dao.Data)

Example 12 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class StatsServiceTest method testIncrementSearchIntergerOverflow.

public void testIncrementSearchIntergerOverflow() {
    Data dataMock = Mockito.mock(Data.class);
    when(dataMock.getDataByName(Values.CACHE_TOTAL_SEARCH, "0")).thenReturn("" + Integer.MAX_VALUE);
    StatsService statsService = new StatsService(dataMock);
    statsService.incrementSearchCount();
    verify(dataMock, times(1)).saveData(Values.CACHE_TOTAL_SEARCH, "1");
}
Also used : Data(com.searchcode.app.dao.Data)

Example 13 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class StatsServiceTest method testIncrementSearchCountTwo.

public void testIncrementSearchCountTwo() {
    Data dataMock = Mockito.mock(Data.class);
    when(dataMock.getDataByName(Values.CACHE_TOTAL_SEARCH, "0")).thenReturn("100");
    StatsService statsService = new StatsService(dataMock);
    statsService.incrementSearchCount();
    verify(dataMock, times(1)).saveData(Values.CACHE_TOTAL_SEARCH, "101");
}
Also used : Data(com.searchcode.app.dao.Data)

Example 14 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class App method preStart.

/**
     * Called on startup to run all the DAO object table creation/migration logic. Slight overhead using this technique.
     * TODO Do the migrations inside the sqlite database so the application does not need to
     */
public static void preStart() {
    // Database migrations
    Data data = Singleton.getData();
    Repo repo = Singleton.getRepo();
    Api api = Singleton.getApi();
    data.createTableIfMissing();
    api.createTableIfMissing();
    repo.createTableIfMissing();
    repo.addSourceToTable();
    repo.addBranchToTable();
    repo.addDataToTable();
}
Also used : Repo(com.searchcode.app.dao.Repo) Data(com.searchcode.app.dao.Data) Api(com.searchcode.app.dao.Api)

Example 15 with Data

use of com.searchcode.app.dao.Data in project searchcode-server by boyter.

the class CodeRouteService method getCode.

public Map<String, Object> getCode(Request request, Response response) {
    Map<String, Object> map = new HashMap<>();
    Repo repo = Singleton.getRepo();
    Data data = Singleton.getData();
    SearchcodeLib scl = Singleton.getSearchcodeLib(data);
    OWASPClassifier owaspClassifier = new OWASPClassifier();
    Cocomo2 coco = new Cocomo2();
    String codeId = request.params(":codeid");
    CodeResult codeResult = this.codeSearcher.getByCodeId(codeId);
    if (codeResult == null) {
        response.redirect("/404/");
        halt();
    }
    List<String> codeLines = codeResult.code;
    StringBuilder code = new StringBuilder();
    StringBuilder lineNos = new StringBuilder();
    String padStr = "";
    for (int total = codeLines.size() / 10; total > 0; total = total / 10) {
        padStr += " ";
    }
    for (int i = 1, d = 10, len = codeLines.size(); i <= len; i++) {
        if (i / d > 0) {
            d *= 10;
            // Del last char
            padStr = padStr.substring(0, padStr.length() - 1);
        }
        code.append("<span id=\"").append(i).append("\"></span>").append(StringEscapeUtils.escapeHtml4(codeLines.get(i - 1))).append("\n");
        lineNos.append(padStr).append("<a href=\"#").append(i).append("\">").append(i).append("</a>").append("\n");
    }
    List<OWASPMatchingResult> owaspResults = new ArrayList<OWASPMatchingResult>();
    if (CommonRouteService.owaspAdvisoriesEnabled()) {
        if (!codeResult.languageName.equals("Text") && !codeResult.languageName.equals("Unknown")) {
            owaspResults = owaspClassifier.classifyCode(codeLines, codeResult.languageName);
        }
    }
    int limit = Integer.parseInt(Properties.getProperties().getProperty(Values.HIGHLIGHT_LINE_LIMIT, Values.DEFAULT_HIGHLIGHT_LINE_LIMIT));
    boolean highlight = Singleton.getHelpers().tryParseInt(codeResult.codeLines, "0") <= limit;
    RepoResult repoResult = repo.getRepoByName(codeResult.repoName);
    if (repoResult != null) {
        map.put("source", repoResult.getSource());
    }
    map.put("fileName", codeResult.fileName);
    // TODO fix this properly code path includes the repo name and should be removed
    String codePath = codeResult.codePath;
    if (codeResult.codePath.contains("/")) {
        codePath = codeResult.codePath.substring(codeResult.codePath.indexOf('/'), codeResult.codePath.length());
    }
    if (!codePath.startsWith("/")) {
        codePath = "/" + codePath;
    }
    map.put("codePath", codePath);
    map.put("codeLength", codeResult.codeLines);
    map.put("linenos", lineNos.toString());
    map.put("languageName", codeResult.languageName);
    map.put("md5Hash", codeResult.md5hash);
    map.put("repoName", codeResult.repoName);
    map.put("highlight", highlight);
    map.put("repoLocation", codeResult.getRepoLocation());
    map.put("codeValue", code.toString());
    map.put("highligher", CommonRouteService.getSyntaxHighlighter());
    map.put("codeOwner", codeResult.getCodeOwner());
    map.put("owaspResults", owaspResults);
    double estimatedEffort = coco.estimateEffort(scl.countFilteredLines(codeResult.getCode()));
    int estimatedCost = (int) coco.estimateCost(estimatedEffort, CommonRouteService.getAverageSalary());
    if (estimatedCost != 0 && !scl.languageCostIgnore(codeResult.getLanguageName())) {
        map.put("estimatedCost", estimatedCost);
    }
    map.put("logoImage", CommonRouteService.getLogo());
    map.put("isCommunity", App.ISCOMMUNITY);
    map.put(Values.EMBED, Singleton.getData().getDataByName(Values.EMBED, Values.EMPTYSTRING));
    return map;
}
Also used : Data(com.searchcode.app.dao.Data) Repo(com.searchcode.app.dao.Repo) RepoResult(com.searchcode.app.model.RepoResult)

Aggregations

Data (com.searchcode.app.dao.Data)22 Repo (com.searchcode.app.dao.Repo)4 CodeMatcher (com.searchcode.app.service.CodeMatcher)3 RepoResult (com.searchcode.app.model.RepoResult)2 CodeSearcher (com.searchcode.app.service.CodeSearcher)2 ModelAndView (spark.ModelAndView)2 Request (spark.Request)2 Gson (com.google.gson.Gson)1 App (com.searchcode.app.App)1 Values (com.searchcode.app.config.Values)1 Api (com.searchcode.app.dao.Api)1 com.searchcode.app.dto (com.searchcode.app.dto)1 CodeResult (com.searchcode.app.dto.CodeResult)1 SearchResult (com.searchcode.app.dto.SearchResult)1 Singleton (com.searchcode.app.service.Singleton)1 TimeCodeSearcher (com.searchcode.app.service.TimeCodeSearcher)1 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)1 CommonRouteService (com.searchcode.app.service.route.CommonRouteService)1 com.searchcode.app.util (com.searchcode.app.util)1 Properties (com.searchcode.app.util.Properties)1