[USERGRID-336] Updating ContentTypeResourceIT
Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/9c81a980 Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/9c81a980 Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/9c81a980 Branch: refs/heads/two-dot-o Commit: 9c81a980dbe488b19f5d18638c4b1465427f6a8e Parents: 8eda8be Author: ryan bridges <[email protected]> Authored: Thu Apr 2 02:29:05 2015 -0400 Committer: ryan bridges <[email protected]> Committed: Fri Apr 17 10:05:17 2015 -0400 ---------------------------------------------------------------------- .../rest/filters/ContentTypeResourceIT.java | 290 +++++++------------ 1 file changed, 97 insertions(+), 193 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/9c81a980/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java ---------------------------------------------------------------------- diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java index 5671249..10909b4 100644 --- a/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java +++ b/stack/rest/src/test/java/org/apache/usergrid/rest/filters/ContentTypeResourceIT.java @@ -17,52 +17,31 @@ package org.apache.usergrid.rest.filters; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; - -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; - -import org.apache.usergrid.rest.AbstractRestIT; -import org.apache.usergrid.rest.TestContextSetup; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.sun.jersey.api.representation.Form; +import org.apache.usergrid.rest.test.resource2point0.AbstractRestIT; import org.apache.usergrid.utils.JsonUtils; import org.apache.usergrid.utils.UUIDUtils; +import org.junit.Ignore; +import org.junit.Test; -import org.apache.http.Header; -import org.apache.http.HttpEntity; -import org.apache.http.HttpHost; -import org.apache.http.HttpResponse; -import org.apache.http.NameValuePair; -import org.apache.http.ParseException; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.util.EntityUtils; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import java.util.List; -import static org.junit.Assert.assertEquals; import static org.apache.usergrid.utils.MapUtils.hashMap; +import static org.junit.Assert.assertEquals; -/** @author tnine */ +/** + * @author tnine + */ // @Ignore("Client login is causing tests to fail due to socket closure by grizzly. Need to re-enable once we're not // using grizzly to test") public class ContentTypeResourceIT extends AbstractRestIT { - @Rule - public TestContextSetup context = new TestContextSetup( this ); - - /** * Creates a simple entity of type game. Does not set the content type. The type should be set to json to match the * body @@ -70,32 +49,22 @@ public class ContentTypeResourceIT extends AbstractRestIT { @Test public void correctHeaders() throws Exception { + String json = JsonUtils.mapToFormattedJsonString(hashMap("name", "Solitaire1")); - Map<String, String> data = hashMap( "name", "Solitaire1" ); + WebResource.Builder builder = app().collection("games").getResource(true) + .queryParam("access_token", this.getAdminToken().getAccessToken()) + .type(MediaType.APPLICATION_JSON_TYPE) + .accept(MediaType.APPLICATION_JSON); - String json = JsonUtils.mapToFormattedJsonString( data ); + ClientResponse clientResponse = builder.post(ClientResponse.class, json); - DefaultHttpClient client = new DefaultHttpClient(); + assertEquals(200, clientResponse.getStatus()); - HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() ); + MultivaluedMap<String, String> headers = clientResponse.getHeaders(); - HttpPost post = new HttpPost( String.format( "/%s/%s/games", context.getOrgUuid(), context.getAppUuid() ) ); - post.setEntity( new StringEntity( json ) ); - post.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + context.getActiveUser().getToken() ); - post.setHeader( HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON ); - post.setHeader( HttpHeaders.CONTENT_TYPE, "*/*" ); - - HttpResponse rsp = client.execute( host, post ); - - printResponse( rsp ); - - assertEquals( 200, rsp.getStatusLine().getStatusCode() ); - - Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE ); - - assertEquals( 1, headers.length ); - - assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() ); + List<String> contentType = headers.get("Content-Type"); + assertEquals(1, contentType.size()); + assertEquals(MediaType.APPLICATION_JSON, contentType.get(0)); } @@ -105,106 +74,83 @@ public class ContentTypeResourceIT extends AbstractRestIT { */ @Test public void textPlainContentType() throws Exception { - Map<String, String> data = hashMap( "name", "Solitaire2" ); - - String json = JsonUtils.mapToFormattedJsonString( data ); - - DefaultHttpClient client = new DefaultHttpClient(); + String json = JsonUtils.mapToFormattedJsonString(hashMap("name", "Solitaire2")); + WebResource.Builder builder = app().getResource(true) + .queryParam("access_token", this.getAdminToken().getAccessToken()) + .accept(MediaType.APPLICATION_JSON) + .type(MediaType.TEXT_PLAIN_TYPE); - HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() ); + ClientResponse clientResponse = builder.post(ClientResponse.class, json); - HttpPost post = new HttpPost( String.format( "/%s/%s/games", context.getOrgUuid(), context.getAppUuid() ) ); - post.setEntity( new StringEntity( json ) ); - post.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + context.getActiveUser().getToken() ); - post.setHeader( HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON ); - post.setHeader( HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN ); + assertEquals(200, clientResponse.getStatus()); - HttpResponse rsp = client.execute( host, post ); + MultivaluedMap<String, String> headers = clientResponse.getHeaders(); - printResponse( rsp ); + List<String> contentType = headers.get("Content-Type"); + assertEquals(1, contentType.size()); + assertEquals(MediaType.APPLICATION_JSON, contentType.get(0)); - assertEquals( 200, rsp.getStatusLine().getStatusCode() ); - - Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE ); - - assertEquals( 1, headers.length ); - - assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() ); } - /** Tests that application/x-www-url-form-encoded works correctly */ + /** + * Tests that application/x-www-url-form-encoded works correctly + */ @Test public void formEncodedContentType() throws Exception { - List<NameValuePair> pairs = new ArrayList<NameValuePair>(); - - pairs.add( new BasicNameValuePair( "organization", "formContentOrg" ) ); - pairs.add( new BasicNameValuePair( "username", "formContentOrg" ) ); - pairs.add( new BasicNameValuePair( "name", "Test User" ) ); - pairs.add( new BasicNameValuePair( "email", UUIDUtils.newTimeUUID() + "@usergrid.org" ) ); - pairs.add( new BasicNameValuePair( "password", "foobar" ) ); - - UrlEncodedFormEntity entity = new UrlEncodedFormEntity( pairs, "UTF-8" ); - DefaultHttpClient client = new DefaultHttpClient(); + Form payload = new Form(); + payload.add("organization", "formContentOrg"); + payload.add("username", "formContentOrg"); + payload.add("name", "Test User"); + payload.add("email", UUIDUtils.newTimeUUID() + "@usergrid.org"); + payload.add("password", "foobar"); - HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() ); + WebResource.Builder builder = app().getResource(true, this.getAdminToken(clientSetup.getSuperuserName(), clientSetup.getSuperuserPassword())) + .accept(MediaType.APPLICATION_JSON) + .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE); - HttpPost post = new HttpPost( "/management/orgs" ); - post.setEntity( entity ); - // post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + context.getActiveUser().getToken()); + ClientResponse clientResponse = builder.post(ClientResponse.class, payload); - post.setHeader( HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED ); + assertEquals(200, clientResponse.getStatus()); - HttpResponse rsp = client.execute( host, post ); + MultivaluedMap<String, String> headers = clientResponse.getHeaders(); - printResponse( rsp ); - - // should be an error, no content type was set - assertEquals( 200, rsp.getStatusLine().getStatusCode() ); - - Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE ); - - assertEquals( 1, headers.length ); - - assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() ); + List<String> contentType = headers.get("Content-Type"); + assertEquals(1, contentType.size()); + assertEquals(MediaType.APPLICATION_JSON, contentType.get(0)); } - /** Tests that application/x-www-url-form-encoded works correctly */ + /** + * Tests that application/x-www-url-form-encoded works correctly + */ @Test @Ignore("This will only pass in tomcat, and shouldn't pass in grizzly") public void formEncodedUrlContentType() throws Exception { - BasicHttpParams params = new BasicHttpParams(); - - params.setParameter( "organization", "formUrlContentOrg" ); - params.setParameter( "username", "formUrlContentOrg" ); - params.setParameter( "name", "Test User" ); - params.setParameter( "email", UUIDUtils.newTimeUUID() + "@usergrid.org" ); - params.setParameter( "password", "foobar" ); - params.setParameter( "grant_type", "password" ); - - DefaultHttpClient client = new DefaultHttpClient(); + String orgName = clientSetup.getOrganizationName().toLowerCase(); + String appName = clientSetup.getAppName().toLowerCase(); - HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() ); + WebResource.Builder builder = resource().path(String.format("/%s/%s", orgName, appName)) + .queryParam("organization", "formUrlContentOrg") + .queryParam("username", "formUrlContentOrg") + .queryParam("name", "Test User") + .queryParam("email", UUIDUtils.newTimeUUID() + "@usergrid.org") + .queryParam("password", "foobar") + .queryParam("grant_type", "password") + .type(MediaType.APPLICATION_FORM_URLENCODED_TYPE); - HttpPost post = new HttpPost( "/management/orgs" ); - post.setParams( params ); + ClientResponse clientResponse = builder.post(ClientResponse.class); - post.setHeader( HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED ); + assertEquals(200, clientResponse.getStatus()); - HttpResponse rsp = client.execute( host, post ); + MultivaluedMap<String, String> headers = clientResponse.getHeaders(); - printResponse( rsp ); + List<String> contentType = headers.get("Content-Type"); + assertEquals(1, contentType.size()); + assertEquals(MediaType.APPLICATION_JSON, contentType.get(0)); - assertEquals( 200, rsp.getStatusLine().getStatusCode() ); - - Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE ); - - assertEquals( 1, headers.length ); - - assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() ); } @@ -215,96 +161,54 @@ public class ContentTypeResourceIT extends AbstractRestIT { @Test public void missingAcceptAndContent() throws Exception { - Map<String, String> data = hashMap( "name", "Solitaire3" ); - - String json = JsonUtils.mapToFormattedJsonString( data ); - - DefaultHttpClient client = new DefaultHttpClient(); - - HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() ); - - HttpPost post = new HttpPost( String.format( "/%s/%s/games", context.getOrgUuid(), context.getAppUuid() ) ); - post.setEntity( new StringEntity( json ) ); - post.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + context.getActiveUser().getToken() ); + WebResource.Builder builder = app().collection("games").getResource(true) + .queryParam("access_token", this.getAdminToken().getAccessToken()) + .type(MediaType.APPLICATION_JSON_TYPE); - HttpResponse rsp = client.execute( host, post ); + ClientResponse clientResponse = builder.post(ClientResponse.class, JsonUtils.mapToJsonString(hashMap("name", "bar"))); - printResponse( rsp ); + assertEquals(200, clientResponse.getStatus()); - assertEquals( 200, rsp.getStatusLine().getStatusCode() ); + MultivaluedMap<String, String> headers = clientResponse.getHeaders(); - Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE ); - - assertEquals( 1, headers.length ); - - assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() ); + List<String> contentType = headers.get("Content-Type"); + assertEquals(1, contentType.size()); + assertEquals(MediaType.APPLICATION_JSON, contentType.get(0)); } /** - * Creates a simple entity of type game. Does not set the content type. The type should be set to json to match the + * Creates a simple entity of type game. Does not set the Accepts header. The type should be set to json to match the * body. Then does a get without Accept type, it should return application/json, not text/csv */ @Test public void noAcceptGet() throws Exception { - Map<String, String> data = hashMap( "name", "bar" ); - - String json = JsonUtils.mapToFormattedJsonString( data ); - - DefaultHttpClient client = new DefaultHttpClient(); - - HttpHost host = new HttpHost( super.getBaseURI().getHost(), super.getBaseURI().getPort() ); - - HttpPost post = new HttpPost( String.format( "/%s/%s/games", context.getOrgUuid(), context.getAppUuid() ) ); - post.setEntity( new StringEntity( json ) ); - post.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + context.getActiveUser().getToken() ); - post.setHeader( HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON ); - post.setHeader( HttpHeaders.CONTENT_TYPE, "*/*" ); - - HttpResponse rsp = client.execute( host, post ); - printResponse( rsp ); + WebResource.Builder builder = app().collection("games").getResource(true) + .queryParam("access_token", this.getAdminToken().getAccessToken()) + .type(MediaType.APPLICATION_JSON_TYPE); - assertEquals( 200, rsp.getStatusLine().getStatusCode() ); + ClientResponse clientResponse = builder.post(ClientResponse.class, JsonUtils.mapToJsonString(hashMap("name", "bar"))); - Header[] headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE ); + assertEquals(200, clientResponse.getStatus()); - assertEquals( 1, headers.length ); + MultivaluedMap<String, String> headers = clientResponse.getHeaders(); - assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() ); + List<String> contentType = headers.get("Content-Type"); + assertEquals(1, contentType.size()); + assertEquals(MediaType.APPLICATION_JSON, contentType.get(0)); //do the get with no content type, it should get set to application/json - HttpGet get = new HttpGet( String.format( "/%s/%s/games", context.getOrgUuid(), context.getAppUuid() ) ); - get.setHeader( HttpHeaders.AUTHORIZATION, "Bearer " + context.getActiveUser().getToken() ); + clientResponse = builder.get(ClientResponse.class); - rsp = client.execute( host, get ); + assertEquals(200, clientResponse.getStatus()); - printResponse( rsp ); + headers = clientResponse.getHeaders(); - assertEquals( 200, rsp.getStatusLine().getStatusCode() ); - - headers = rsp.getHeaders( HttpHeaders.CONTENT_TYPE ); - - assertEquals( 1, headers.length ); - - assertEquals( MediaType.APPLICATION_JSON, headers[0].getValue() ); + contentType = headers.get("Content-Type"); + assertEquals(1, contentType.size()); + assertEquals(MediaType.APPLICATION_JSON, contentType.get(0)); } - private void printResponse( HttpResponse rsp ) throws ParseException, IOException { - HttpEntity entity = rsp.getEntity(); - - System.out.println( "----------------------------------------" ); - System.out.println( rsp.getStatusLine() ); - - Header[] headers = rsp.getAllHeaders(); - for ( int i = 0; i < headers.length; i++ ) { - System.out.println( headers[i] ); - } - System.out.println( "----------------------------------------" ); - - if ( entity != null ) { - System.out.println( EntityUtils.toString( entity ) ); - } - } }
