/tests/src/test/java/org/sigmah/endtoend/ApiServiceJsonIT.java

http://sigma-h.googlecode.com/ · Java · 48 lines · 28 code · 10 blank · 10 comment · 0 complexity · 2776c3b2ae77e61bf1bb51718cf7aadf MD5 · raw file

  1. /*
  2. * All Sigmah code is released under the GNU General Public License v3
  3. * See COPYRIGHT.txt and LICENSE.txt.
  4. */
  5. package org.sigmah.endtoend;
  6. import junit.framework.Assert;
  7. import org.apache.commons.httpclient.HttpClient;
  8. import org.apache.commons.httpclient.methods.PostMethod;
  9. import org.apache.commons.httpclient.methods.StringRequestEntity;
  10. import org.json.JSONException;
  11. import org.json.JSONTokener;
  12. import org.junit.Test;
  13. import java.io.IOException;
  14. /**
  15. *
  16. * Integration test that verifies that the API service can be accessed through JSON.
  17. *
  18. * @author Alex Bertram
  19. */
  20. public class ApiServiceJsonIT {
  21. private static final String ENDPOINT_URL = "http://localhost:9090/activityinfo/api";
  22. @Test
  23. public void testAuthentication() throws IOException, JSONException {
  24. HttpClient client = new HttpClient();
  25. PostMethod method = new PostMethod(ENDPOINT_URL);
  26. method.setRequestEntity(new StringRequestEntity(
  27. "{authenticate: {email: 'akbertram@gmail.com', password: 'mzuri787'}}",
  28. "application/json",
  29. "UTF-8"));
  30. client.executeMethod(method);
  31. Assert.assertEquals("status code", 200, method.getStatusCode());
  32. String jsonResult = method.getResponseBodyAsString();
  33. JSONTokener tokenizer = new JSONTokener(jsonResult);
  34. String authToken = (String) tokenizer.nextValue();
  35. System.out.println("token = " + authToken);
  36. Assert.assertEquals("token length", 32, authToken.length());
  37. }
  38. }