WebResource resource = client.resource("http://www.example.com/customers"); ClientResponse response = resource.type("application/xml");).put(ClientResponse.class, "<customer>...</customer."); System.out.println(response);
public void upload(String url, String fileName) { InputStream stream = getClass().getClassLoader().getResourceAsStream(fileName); FormDataMultiPart part = new FormDataMultiPart().field("file", stream, MediaType.TEXT_PLAIN_TYPE); WebResource resource = Client.create().resource(url); String response = resource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(String.class, part); assertEquals("Hello, World", response); }
private Builder getWebResourceBuilder(URI URI, Object entity) { return client.resource(URI).type(MediaType.APPLICATION_JSON).entity(entity).accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON); }
public Repository getRepository(String url) { String[] urlParts = url.split("/"); String owner = urlParts[urlParts.length - 2]; String name = urlParts[urlParts.length - 1]; String path = String.format(REPOSITORY_PATH, owner, name); WebResource resource = client.resource(GITHUB_URL).path(path); return resource.type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .header("Authorization", authorizationHeader) .get(Repository.class); }
public String getCommitDescription(String commitUrl) { String[] commitUrlParts = commitUrl.split("/"); String owner = commitUrlParts[commitUrlParts.length - 4]; String repository = commitUrlParts[commitUrlParts.length - 3]; String commit = commitUrlParts[commitUrlParts.length - 1]; String path = String.format(COMMIT_PATH, owner, repository, commit); WebResource resource = client.resource(GITHUB_URL).path(path); Commit response = resource.type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .header("Authorization", authorizationHeader) .get(Commit.class); return response.getMessage(); }
@Override public void register( final Runner runner ) { WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, runner ); Boolean result = resource.path( coordinatorFig.getRunnersRegisterPath() ) .type( MediaType.APPLICATION_JSON ).post( Boolean.class, runner ); LOG.debug( "Got back results from register post = {}", result ); }
@Override public int getNextRunNumber( final Project project ) { WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, me ); Integer result = resource.path( coordinatorFig.getRunNextPath() ) .type( MediaType.APPLICATION_JSON_TYPE ) .get( Integer.class ); LOG.debug( "Got back result from next run number get = {}", result ); return result; } }
@Override public List<ICoordinatedCluster> getClusters() { if( RunnerConfig.isTestMode() ) { return Collections.emptyList(); } WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); StringBuilder sb = new StringBuilder(); sb.append( coordinatorFig.getUsername() ) .append( "/" ) .append( project.getGroupId() ) .append( "/" ) .append( project.getArtifactId() ) .append( "/" ) .append( project.getVersion() ) .append( "/" ) .append( project.getVcsVersion() ); return resource.path( coordinatorFig.getPropertiesPath() ) .path( sb.toString() ) .type( MediaType.APPLICATION_JSON ) .accept( MediaType.APPLICATION_JSON ) .get( new GenericType<List<ICoordinatedCluster>>() { } ); } }
@Override public List<Runner> getRunners() { // get a list of all runners associated with this project WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, me ); List<Runner> runners = resource.path( coordinatorFig.getRunnersListPath() ) .type( MediaType.APPLICATION_JSON ) .accept( MediaType.APPLICATION_JSON_TYPE ) .get( new GenericType<List<Runner>>() {} ); LOG.debug( "Got back runners list = {}", runners ); return runners; }
@Override public void unregister( final Runner runner ) { if ( RunnerConfig.isTestMode() ) { return; } WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, runner ); String result = resource.path( coordinatorFig.getRunnersUnregisterPath() ) .type( MediaType.TEXT_PLAIN ).post( String.class ); LOG.debug( "Got back results from unregister post = {}", result ); }
@Override public void store( final Project project, final Summary summary, final File resultsFile, final Class<?> testClass ) throws FileNotFoundException, MalformedURLException { Preconditions.checkNotNull( summary, "The summary argument cannot be null." ); // upload the results file InputStream in = new FileInputStream( resultsFile ); FormDataMultiPart part = new FormDataMultiPart(); part.field( FILENAME, resultsFile.getName() ); FormDataBodyPart body = new FormDataBodyPart( CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE ); part.bodyPart( body ); WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, me ); String result = resource.path( coordinatorFig.getStoreResultsPath() ) .queryParam( RUN_ID, summary.getRunId() ) .queryParam( RUN_NUMBER, "" + summary.getRunNumber() ) .type( MediaType.MULTIPART_FORM_DATA_TYPE ) .post( String.class, part ); LOG.debug( "Got back result from results file store = {}", result ); }
public void addCommitComment(Repository repository, Commit commit, String comment) { try { String path = String.format(COMMENT_PATH, repository.getOwner().getName(), repository.getName(), commit.getSha()); WebResource resource = client.resource(GITHUB_URL).path(path); ClientResponse response = resource.type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON) .header("Authorization", authorizationHeader) .entity(new CommitComment(comment)) .post(ClientResponse.class); if (response.getStatus() < 200 || response.getStatus() >=300) { logger.warn("Commit comment failed: " + response.getClientResponseStatus().getReasonPhrase()); } } catch (UniformInterfaceException | ClientHandlerException e) { logger.warn("Comment failed", e); } }
@Override public boolean hasCompleted( final Runner runner, final Project project, final int runNumber, final Class<?> testClass ) { // get run status information WebResource resource = Client.create().resource( coordinatorFig.getEndpoint() ); resource = addQueryParameters( resource, project, runner ); ClientResponse result = resource.path( coordinatorFig.getRunCompletedPath() ) .queryParam( RUNNER_HOSTNAME, runner.getHostname() ) .queryParam( COMMIT_ID, project.getVcsVersion() ) .queryParam( RUN_NUMBER, String.valueOf( runNumber ) ) .queryParam( TEST_CLASS, testClass.getName() ) .type( MediaType.APPLICATION_JSON ) .get( ClientResponse.class ); if( result.getStatus() != Response.Status.CREATED.getStatusCode() ) { LOG.error( "Could not get if run has completed status from coordinator, HTTP status: {}", result.getStatus() ); return false; } return result.getEntity( Boolean.class ); }
static void testRunManagerNext( TestParams testParams ) { Integer next = testParams.addQueryParameters( QUERY_PARAMS ) .setEndpoint( RunManagerResource.ENDPOINT ) .newWebResource() .path( "/next" ) .type( MediaType.APPLICATION_JSON_TYPE ) .accept( MediaType.APPLICATION_JSON ) .get( Integer.class ); assertEquals( 1, next.intValue() ); }
static void testDestroy( TestParams testParams ) { ClientResponse response = testParams.addQueryParameters( QUERY_PARAMS ) .setEndpoint( DestroyResource.ENDPOINT ) .newWebResource() .path( "/stack" ) .type( MediaType.APPLICATION_JSON ) .accept( MediaType.APPLICATION_JSON ) .post( ClientResponse.class ); assertEquals( Response.Status.CREATED.getStatusCode(), response.getStatus() ); assertEquals( "\"JarNotFound\"", response.getEntity( String.class ) ); }
static void testRunnerRegistryList(TestParams testParams) { List<Runner> runnerList = testParams.addQueryParameters(QUERY_PARAMS) .setEndpoint(RunnerRegistryResource.ENDPOINT) .newWebResource() .path("/list") .type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .get(new GenericType<List<Runner>>() { }); assertNotNull(runnerList); assertEquals(0, runnerList.size()); }
static void testSetup(TestParams testParams) { ClientResponse response = testParams.addQueryParameters(QUERY_PARAMS) .setEndpoint(SetupResource.ENDPOINT) .newWebResource() .queryParam(RestParams.RUNNER_COUNT, "5") .path("/stack") .type(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .post(ClientResponse.class); assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus()); assertEquals("\"JarNotFound\"", response.getEntity(String.class)); }
static void testRunCompleted( TestParams testParams ) { ClientResponse response = testParams.addQueryParameters( QUERY_PARAMS ) .setEndpoint( RunManagerResource.ENDPOINT ) .newWebResource() .queryParam( RestParams.RUNNER_HOSTNAME, "localhost" ) .queryParam( RestParams.RUN_NUMBER, "1" ) .queryParam( RestParams.TEST_CLASS, "org.apache.usergrid.chop.example.MechanicalWatchTest" ) .path( "/completed" ) .type( MediaType.APPLICATION_JSON ) .accept( MediaType.APPLICATION_JSON ) .get( ClientResponse.class ); assertEquals( Response.Status.CREATED.getStatusCode(), response.getStatus() ); assertEquals( Boolean.TRUE, response.getEntity( Boolean.class ) ); }
static void testStoreResults( TestParams testParams ) throws Exception { FormDataMultiPart part = new FormDataMultiPart(); File tmpFile = File.createTempFile("results", "tmp"); FileInputStream in = new FileInputStream( tmpFile ); FormDataBodyPart body = new FormDataBodyPart( RestParams.CONTENT, in, MediaType.APPLICATION_OCTET_STREAM_TYPE ); part.bodyPart( body ); ClientResponse response = testParams.addQueryParameters( QUERY_PARAMS ) .setEndpoint( RunManagerResource.ENDPOINT ) .newWebResource() .queryParam( RestParams.RUNNER_HOSTNAME, "localhost" ) .queryParam( RestParams.RUN_ID, "112316437" ) .queryParam( RestParams.RUN_NUMBER, "3" ) .path( "/store" ) .type( MediaType.MULTIPART_FORM_DATA_TYPE ) .accept( MediaType.APPLICATION_JSON ) .post( ClientResponse.class, part ); tmpFile.delete(); assertEquals( Response.Status.CREATED.getStatusCode(), response.getStatus() ); assertEquals( UploadResource.SUCCESSFUL_TEST_MESSAGE, response.getEntity( String.class ) ); }
static void testRunnerRegistryRegister(TestParams testParams) { /* * Even though in test mode the runner is not used, a runner must * be sent over because the method is expecting the Runner as JSON. */ RunnerBuilder builder = new RunnerBuilder(); builder.setTempDir(".") .setServerPort(19023) .setUrl("https://localhost:19023") .setHostname("foobar") .setIpv4Address("127.0.0.1"); Boolean result = testParams.addQueryParameters(QUERY_PARAMS) .setEndpoint(RunnerRegistryResource.ENDPOINT) .newWebResource() .path("/register") .type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON_TYPE) .post(Boolean.class, builder.getRunner()); assertFalse(result); }