Search in sources :

Example 1 with Request

use of spark.Request in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepositoryIndexApiNoMatchinRepo.

public void testRepositoryIndexApiNoMatchinRepo() {
    JobService mockJobService = Mockito.mock(JobService.class);
    Request mockRequest = Mockito.mock(Request.class);
    Repo mockRepo = Mockito.mock(Repo.class);
    when(mockJobService.forceEnqueue(Matchers.<RepoResult>anyObject())).thenReturn(true);
    when(mockRequest.queryParams("repoUrl")).thenReturn("test");
    ApiRouteService apiRouteService = new ApiRouteService(null, mockJobService, mockRepo, null, null);
    apiRouteService.apiEnabled = true;
    ApiResponse apiResponse = apiRouteService.repositoryIndex(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("Was unable to find repository test");
    assertThat(apiResponse.isSucessful()).isEqualTo(false);
}
Also used : Repo(com.searchcode.app.dao.Repo) ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Request(spark.Request) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 2 with Request

use of spark.Request in project spark by perwendel.

the class Routes method execute.

static void execute(RouteContext context) throws Exception {
    Object content = context.body().get();
    RouteMatch match = context.routeMatcher().find(context.httpMethod(), context.uri(), context.acceptType());
    Object target = null;
    if (match != null) {
        target = match.getTarget();
    } else if (context.httpMethod() == HttpMethod.head && context.body().notSet()) {
        // See if get is mapped to provide default head mapping
        content = context.routeMatcher().find(HttpMethod.get, context.uri(), context.acceptType()) != null ? "" : null;
    }
    if (target != null) {
        Object result = null;
        if (target instanceof RouteImpl) {
            RouteImpl route = ((RouteImpl) target);
            if (context.requestWrapper().getDelegate() == null) {
                Request request = RequestResponseFactory.create(match, context.httpRequest());
                context.requestWrapper().setDelegate(request);
            } else {
                context.requestWrapper().changeMatch(match);
            }
            context.responseWrapper().setDelegate(context.response());
            Object element = route.handle(context.requestWrapper(), context.responseWrapper());
            if (!context.responseWrapper().isRedirected()) {
                result = route.render(element);
            }
        }
        if (result != null) {
            content = result;
            if (content instanceof String) {
                String contentStr = (String) content;
                if (!contentStr.equals("")) {
                    context.responseWrapper().body(contentStr);
                }
            }
        }
    }
    context.body().set(content);
}
Also used : RouteImpl(spark.RouteImpl) Request(spark.Request) RouteMatch(spark.routematch.RouteMatch)

Example 3 with Request

use of spark.Request in project cloudbreak by hortonworks.

the class AmbariClusterResponse method handle.

@Override
public Object handle(Request request, Response response) {
    response.type("text/plain");
    ObjectNode rootNode = JsonNodeFactory.instance.objectNode();
    Set<String> hostNames = instanceMap.values().stream().map(cv -> HostNameUtil.generateHostNameByIp(cv.getMetaData().getPrivateIp())).collect(Collectors.toSet());
    rootNode.putObject("hosts").set("Hosts", getObjectMapper().valueToTree(new Hosts(hostNames, "HEALTHY")));
    ArrayNode items = rootNode.putArray("items");
    items.addObject().set("Clusters", getObjectMapper().valueToTree(new Clusters(clusterName)));
    return rootNode;
}
Also used : HostNameUtil(com.sequenceiq.it.util.HostNameUtil) Set(java.util.Set) Hosts(com.sequenceiq.it.spark.ambari.model.Hosts) Clusters(com.sequenceiq.it.spark.ambari.model.Clusters) Collectors(java.util.stream.Collectors) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Request(spark.Request) Map(java.util.Map) ITResponse(com.sequenceiq.it.spark.ITResponse) Response(spark.Response) CloudVmMetaDataStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus) Hosts(com.sequenceiq.it.spark.ambari.model.Hosts) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Clusters(com.sequenceiq.it.spark.ambari.model.Clusters) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 4 with Request

use of spark.Request in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepositoryIndexMatchingRepo.

public void testRepositoryIndexMatchingRepo() {
    JobService mockJobService = mock(JobService.class);
    Request mockRequest = mock(Request.class);
    SQLiteRepo mockSQLiteRepo = mock(SQLiteRepo.class);
    when(mockJobService.forceEnqueue(Matchers.<RepoResult>anyObject())).thenReturn(true);
    when(mockRequest.queryParams("repoUrl")).thenReturn("http://test/");
    when(mockSQLiteRepo.getRepoByUrl("http://test/")).thenReturn(Optional.of(new RepoResult()));
    ApiRouteService apiRouteService = new ApiRouteService(null, mockJobService, mockSQLiteRepo, null, null, null, new Helpers(), new LoggerWrapper());
    apiRouteService.apiEnabled = true;
    ApiResponse apiResponse = apiRouteService.repositoryIndex(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("Enqueued repository http://test/");
    assertThat(apiResponse.isSucessful()).isEqualTo(true);
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Helpers(com.searchcode.app.util.Helpers) LoggerWrapper(com.searchcode.app.util.LoggerWrapper) Request(spark.Request) SQLiteRepo(com.searchcode.app.dao.SQLiteRepo) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Example 5 with Request

use of spark.Request in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepoDeleteNoAuthReponame.

public void testRepoDeleteNoAuthReponame() {
    Request mockRequest = mock(Request.class);
    SQLiteRepo mockSQLiteRepo = mock(SQLiteRepo.class);
    DataService dataServiceMock = mock(DataService.class);
    when(mockSQLiteRepo.getRepoByName("unit-test")).thenReturn(Optional.of(new RepoResult()));
    ApiRouteService apiRouteService = new ApiRouteService(null, null, mockSQLiteRepo, dataServiceMock, null, null, new Helpers(), new LoggerWrapper());
    apiRouteService.apiEnabled = true;
    apiRouteService.apiAuth = false;
    when(mockRequest.queryParams("reponame")).thenReturn("unit-test");
    ApiResponse apiResponse = apiRouteService.repoDelete(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("repository queued for deletion");
    assertThat(apiResponse.isSucessful()).isTrue();
    verify(dataServiceMock, times(1)).addToPersistentDelete("");
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) Helpers(com.searchcode.app.util.Helpers) LoggerWrapper(com.searchcode.app.util.LoggerWrapper) Request(spark.Request) SQLiteRepo(com.searchcode.app.dao.SQLiteRepo) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Aggregations

Request (spark.Request)70 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)35 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)33 Helpers (com.searchcode.app.util.Helpers)31 LoggerWrapper (com.searchcode.app.util.LoggerWrapper)31 SQLiteRepo (com.searchcode.app.dao.SQLiteRepo)28 ApiResponse (com.searchcode.app.dto.api.ApiResponse)28 Response (spark.Response)17 RepoResult (com.searchcode.app.model.RepoResult)11 AdminRouteService (com.searchcode.app.service.route.AdminRouteService)8 CodeRouteService (com.searchcode.app.service.route.CodeRouteService)7 Collectors (java.util.stream.Collectors)5 IndexService (com.searchcode.app.service.index.IndexService)4 IOException (java.io.IOException)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 Spark (spark.Spark)4 ValidatorResult (com.searchcode.app.model.ValidatorResult)3 ApiController (com.thoughtworks.go.api.ApiController)3 ApiVersion (com.thoughtworks.go.api.ApiVersion)3 JsonReader (com.thoughtworks.go.api.representers.JsonReader)3