Created
May 20, 2013 15:27
-
-
Save kpiwko/5612949 to your computer and use it in GitHub Desktop.
Various approaches to test REST like API with Arquillian
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void registerPushApp() throws Exception { | |
HttpResponse response = Requests.post().uri(root, "rest/applications").accept("application/json") | |
.json(new JSONObject().put("name", "MyApp4").put("description", "awesome app v4")).execute(); | |
assertThat(response, is(not(nullValue()))); | |
assertThat(response.getStatusLine(), is(not(nullValue()))); | |
assertThat(response.getStatusLine().getStatusCode(), is(200)); | |
JSONObject jsonResult = new JsonHttpResponseHandler().handle(response); | |
assertThat(jsonResult.get("id"), is(not(nullValue()))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void registerPushAppRaw() throws Exception { | |
// prepare request | |
HttpClient client = new DefaultHttpClient(); | |
HttpPost post = new HttpPost(); | |
post.setHeader("Accept", "application/json"); | |
post.setHeader("Content-type", "application/json"); | |
post.setURI(UriTool.append(contextPath, "rest/applications")); | |
StringEntity entity = new StringEntity("{\"name\" : \"MyApp\", \"description\": \"awesome app\" }"); | |
entity.setContentType("application/json"); | |
post.setEntity(entity); | |
// get response | |
HttpResponse response = client.execute(post); | |
assertThat(response, is(not(nullValue()))); | |
assertThat(response.getStatusLine(), is(not(nullValue()))); | |
assertThat(response.getStatusLine().getStatusCode(), is(200)); | |
String responseMsg = EntityUtils.toString(response.getEntity(), "UTF-8"); | |
System.err.println(responseMsg); | |
} | |
@Test | |
public void registerPushAppGson() throws Exception { | |
HttpClient client = new DefaultHttpClient(); | |
HttpPost post = new HttpPost(); | |
post.setHeader("Accept", "application/json"); | |
post.setHeader("Content-type", "application/json"); | |
post.setURI(UriTool.append(contextPath, "rest/applications")); | |
PushApplication pushApp = new PushApplication(); | |
pushApp.setName("MyApp2"); | |
pushApp.setDescription("awesome app v2"); | |
Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.STATIC).create(); | |
StringEntity entity = new StringEntity(gson.toJson(pushApp)); | |
entity.setContentType("application/json"); | |
post.setEntity(entity); | |
System.err.println(gson.toJson(pushApp)); | |
// get response | |
HttpResponse response = client.execute(post); | |
assertThat(response, is(not(nullValue()))); | |
assertThat(response.getStatusLine(), is(not(nullValue()))); | |
assertThat(response.getStatusLine().getStatusCode(), is(200)); | |
String responseMsg = EntityUtils.toString(response.getEntity(), "UTF-8"); | |
PushApplication pushAppResults = new Gson().fromJson(responseMsg, PushApplication.class); | |
assertThat(pushAppResults.getId(), is(not(nullValue()))); | |
System.err.println(responseMsg); | |
} | |
@Test | |
public void registerPushAppGsonGeneric() throws Exception { | |
HttpClient client = new DefaultHttpClient(); | |
HttpPost post = new HttpPost(); | |
post.setHeader("Accept", "application/json"); | |
post.setHeader("Content-type", "application/json"); | |
post.setURI(UriTool.append(contextPath, "rest/applications")); | |
Map<String, Object> pushApp = new HashMap<String, Object>(); | |
pushApp.put("name", "MyApp3"); | |
pushApp.put("description", "awesome app v3"); | |
StringEntity entity = new StringEntity(new Gson().toJson(pushApp)); | |
entity.setContentType("application/json"); | |
post.setEntity(entity); | |
System.err.println(new Gson().toJson(pushApp)); | |
// get response | |
HttpResponse response = client.execute(post); | |
assertThat(response, is(not(nullValue()))); | |
assertThat(response.getStatusLine(), is(not(nullValue()))); | |
assertThat(response.getStatusLine().getStatusCode(), is(200)); | |
String responseMsg = EntityUtils.toString(response.getEntity(), "UTF-8"); | |
Type collectionType = new TypeToken<Map<String, Object>>() { | |
}.getType(); | |
Map<String, Object> pushAppResults = new Gson().fromJson(responseMsg, collectionType); | |
assertThat(pushAppResults.get("id"), is(not(nullValue()))); | |
System.err.println(responseMsg); | |
} | |
@Test | |
public void registerPushAppJSON() throws Exception { | |
HttpClient client = new DefaultHttpClient(); | |
HttpPost post = new HttpPost(); | |
post.setHeader("Accept", "application/json"); | |
post.setHeader("Content-type", "application/json"); | |
post.setURI(UriTool.append(contextPath, "rest/applications")); | |
JSONObject json = new JSONObject().put("name", "MyApp4").put("description", "awesome app v4"); | |
StringEntity entity = new StringEntity(json.toString()); | |
entity.setContentType("application/json"); | |
post.setEntity(entity); | |
System.err.println(json.toString()); | |
// get response | |
HttpResponse response = client.execute(post); | |
assertThat(response, is(not(nullValue()))); | |
assertThat(response.getStatusLine(), is(not(nullValue()))); | |
assertThat(response.getStatusLine().getStatusCode(), is(200)); | |
String responseMsg = EntityUtils.toString(response.getEntity(), "UTF-8"); | |
JSONObject jsonResult = new JSONObject(responseMsg); | |
assertThat(jsonResult.get("id"), is(not(nullValue()))); | |
System.err.println(responseMsg); | |
} | |
private static class UriTool { | |
static URI append(URL root, String path) { | |
try { | |
return new URI(root.toURI().toString() + path); | |
} catch (URISyntaxException e) { | |
throw new IllegalArgumentException("Unable to combine URL paths together"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void registerPushApp() { | |
def json = new JsonBuilder() | |
given().contentType("application/json") | |
// FIXME accept type is crucial here, | |
.header("Accept", "application/json") | |
.body( json { | |
name "MyApp" | |
description "awesome app" | |
} | |
) | |
.expect() | |
.statusCode(200).body("id", is(not(nullValue()))) | |
.when() | |
.post(root.toString() + "rest/applications").asString(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void registerPushApp() throws Exception { | |
given() | |
.contentType("application/json") | |
// FIXME accept type is crucial here, | |
.header("Accept", "application/json") | |
.body("{'name': 'MyApp', 'description' : 'awesome app'}".replaceAll("'", "\"")). | |
expect() | |
.statusCode(200).body("id", is(not(nullValue()))). | |
when() | |
.post(root.toString() + "rest/applications").asString(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ArquillianResource URL root; | |
def "Registering a push application"() { | |
given: "Application My App is about to be registered" | |
def json = new JsonBuilder() | |
def request = RestAssured.given() | |
.contentType("application/json") | |
.header("Accept", "application/json") | |
.body(json { | |
name: "MyApp" | |
description: "awesome app" | |
}) | |
when: "Money has been transfered from one account to another" | |
def response = RestAssured.given().spec(request).post(root.toString() + "rest/applications") | |
then: "Response code 200 is returned" | |
response.statusCode() == 200 | |
and: "Push App Id is not null" | |
response.body().path("id") != null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment