PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 6package org.sigmah.endtoend;
 7
 8import junit.framework.Assert;
 9import org.apache.commons.httpclient.HttpClient;
10import org.apache.commons.httpclient.methods.PostMethod;
11import org.apache.commons.httpclient.methods.StringRequestEntity;
12import org.json.JSONException;
13import org.json.JSONTokener;
14import org.junit.Test;
15
16import java.io.IOException;
17
18/**
19 *
20 * Integration test that verifies that the API service can be accessed through JSON.
21 *
22 * @author Alex Bertram
23 */
24public class ApiServiceJsonIT {
25
26    private static final String ENDPOINT_URL = "http://localhost:9090/activityinfo/api";
27
28    @Test
29    public void testAuthentication() throws IOException, JSONException {
30        HttpClient client = new HttpClient();
31        PostMethod method = new PostMethod(ENDPOINT_URL);
32        method.setRequestEntity(new StringRequestEntity(
33                "{authenticate: {email: 'akbertram@gmail.com', password: 'mzuri787'}}",
34                "application/json",
35                "UTF-8"));
36        client.executeMethod(method);
37
38        Assert.assertEquals("status code", 200, method.getStatusCode());
39
40        String jsonResult = method.getResponseBodyAsString();
41
42        JSONTokener tokenizer = new JSONTokener(jsonResult);
43        String authToken = (String) tokenizer.nextValue();
44        System.out.println("token = " + authToken);
45
46        Assert.assertEquals("token length", 32, authToken.length());
47    }
48}