RunnerAdminService extends Service { @ApiOperation(value = "Get all registered runners", notes = "Get all registered runners", response = RunnerServer.class, responseContainer = "List", position = 3) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this method"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_REGISTERED_RUNNER_SERVER) @GET @Produces(MediaType.APPLICATION_JSON) @Path("/server") public List<RunnerServer> getRegisteredServers() { final List<RemoteRunnerServer> runnerServers = runner.getRegisterRunnerServers(); final List<RunnerServer> result = new LinkedList<>(); final DtoFactory dtoFactory = DtoFactory.getInstance(); for (RemoteRunnerServer runnerServer : runnerServers) { final RunnerServer runnerServerDTO = dtoFactory.createDto(RunnerServer.class); runnerServerDTO.withUrl(runnerServer.getBaseUrl()); try { runnerServerDTO.withDedicated(runnerServer.isDedicated()) .withWorkspace(runnerServer.getAssignedWorkspace()) .withProject(runnerServer.getAssignedProject()); final List<Link> adminLinks = new LinkedList<>(); for (String linkRel : SERVER_LINK_RELS) { final Link link = runnerServer.getLink(linkRel); if (link != null) { if (Constants.LINK_REL_RUNNER_STATE.equals(linkRel)) { for (RunnerDescriptor runner : runnerServer.getRunnerDescriptors()) { final String href = link.getHref(); final String hrefWithRunner = href + ((href.indexOf('?') > 0 ? '&' : '?') + "runner=" + runner.getName()); final Link linkCopy = dtoFactory.clone(link); linkCopy.getParameters().clear(); linkCopy.setHref(hrefWithRunner); adminLinks.add(linkCopy); } } else { adminLinks.add(link); } } } runnerServerDTO.withDescription(runnerServer.getServiceDescriptor().getDescription()) .withServerState(runnerServer.getServerState()) .withLinks(adminLinks); } catch (IOException | ServerException e) { LOG.error(e.getMessage(), e); } result.add(runnerServerDTO); } return result; }  @ApiOperation(value = "Register a new runner", notes = "Register a new runner service", response = RunnerServerRegistration.class, position = 1) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this method"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_REGISTER_RUNNER_SERVER) @POST @Path("/server/register") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response registerRunnerServer(@ApiParam(value = "JSON with runner location and options")
                                         RunnerServerRegistration registration); @ApiOperation(value = "Unregister runner", notes = "Unregister runner service", response = RunnerServerLocation.class, position = 2) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this method"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_UNREGISTER_RUNNER_SERVER) @POST @Path("/server/unregister") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response unregisterRunnerServer(@ApiParam(value = "Location of a runner to unregister", required = true)
                                           RunnerServerLocation location); @ApiOperation(value = "Get all registered runners", notes = "Get all registered runners", response = RunnerServer.class, responseContainer = "List", position = 3) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this method"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_REGISTERED_RUNNER_SERVER) @GET @Produces(MediaType.APPLICATION_JSON) @Path("/server") List<RunnerServer> getRegisteredServers(); @ApiOperation(value = "Check runner queue", notes = "Check runner queue. JSON with queue state and details is returned", response = ApplicationProcessDescriptor.class, responseContainer = "List", position = 4) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 403, message = "User not authorized to call this method"), @ApiResponse(code = 500, message = "Internal Server Error")}) @GenerateLink(rel = Constants.LINK_REL_RUNNER_TASKS) @GET @Produces(MediaType.APPLICATION_JSON) @Path("/queue") List<ApplicationProcessDescriptor> getTasks();  }
@Test public void testGetServers() throws Exception { String serverUrl = "http: List<RemoteRunnerServer> servers = new ArrayList<>(1); RemoteRunnerServer server1 = mock(RemoteRunnerServer.class); doReturn(serverUrl).when(server1).getBaseUrl(); doReturn(dto(ServiceDescriptor.class)).when(server1).getServiceDescriptor(); servers.add(server1); List<RunnerDescriptor> runners = new ArrayList<>(1); RunnerDescriptor runner1 = dto(RunnerDescriptor.class).withName("java/web"); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("tomcat7")); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("jboss7")); runners.add(runner1); doReturn(runners).when(server1).getRunnerDescriptors(); doReturn(servers).when(runQueue).getRegisterRunnerServers(); List<RunnerServer> _servers = service.getRegisteredServers(); assertEquals(_servers.size(), 1); RunnerServer _server = _servers.get(0); assertEquals(_server.getUrl(), serverUrl); }
@Test public void testGetServersIfOneServerUnavailable() throws Exception { String serverUrl1 = "http: List<RemoteRunnerServer> servers = new ArrayList<>(1); RemoteRunnerServer server1 = mock(RemoteRunnerServer.class); doReturn(serverUrl1).when(server1).getBaseUrl(); ServiceDescriptor serviceDescriptor = dto(ServiceDescriptor.class); Link availableLink = dto(Link.class).withRel(Constants.LINK_REL_AVAILABLE_RUNNERS).withMethod(HttpMethod.GET).withHref( serverUrl1 + "/available"); Link serverStateLink = dto(Link.class).withRel(Constants.LINK_REL_SERVER_STATE).withMethod(HttpMethod.GET).withHref( serverUrl1 + "/server-state"); Link runnerStateLink = dto(Link.class).withRel(Constants.LINK_REL_RUNNER_STATE).withMethod(HttpMethod.GET).withHref(serverUrl1 + "/state"); serviceDescriptor.getLinks().add(availableLink); serviceDescriptor.getLinks().add(serverStateLink); serviceDescriptor.getLinks().add(runnerStateLink); doReturn(serviceDescriptor).when(server1).getServiceDescriptor(); doReturn(availableLink).when(server1).getLink(eq(Constants.LINK_REL_AVAILABLE_RUNNERS)); doReturn(serverStateLink).when(server1).getLink(eq(Constants.LINK_REL_SERVER_STATE)); doReturn(runnerStateLink).when(server1).getLink(eq(Constants.LINK_REL_RUNNER_STATE)); doReturn(dto(ServerState.class)).when(server1).getServerState(); servers.add(server1); List<RunnerDescriptor> runners = new ArrayList<>(1); RunnerDescriptor runner1 = dto(RunnerDescriptor.class).withName("java/web"); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("tomcat7")); runner1.getEnvironments().add(dto(RunnerEnvironment.class).withId("jboss7")); runners.add(runner1); doReturn(runners).when(server1).getRunnerDescriptors(); RemoteRunnerServer server2 = mock(RemoteRunnerServer.class); String serverUrl2 = "http: doReturn(serverUrl2).when(server2).getBaseUrl(); doThrow(new RunnerException("Connection refused")).when(server2).getRunnerDescriptors(); doThrow(new RunnerException("Connection refused")).when(server2).getServiceDescriptor(); doThrow(new RunnerException("Connection refused")).when(server2).getServerState(); servers.add(server2); doReturn(servers).when(runQueue).getRegisterRunnerServers(); List<RunnerServer> _servers = service.getRegisteredServers(); assertEquals(_servers.size(), 2); RunnerServer _server1 = _servers.get(0); assertEquals(_server1.getUrl(), serverUrl1); assertNotNull(_server1.getServerState()); assertEquals(_server1.getLinks().size(), 3); RunnerServer _server2 = _servers.get(1); assertEquals(_server2.getUrl(), serverUrl2); assertNull(_server2.getServerState()); assertTrue(_server2.getLinks().isEmpty()); }