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);
}
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);
}
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;
}
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);
}
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("");
}
Aggregations