Skip to content
Snippets Groups Projects
Commit b34d3cda authored by srosse's avatar srosse
Browse files

OO-254: move some REST Unit tests to HttpClient 4.x

parent 3312be31
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
......@@ -20,19 +20,20 @@
package org.olat.restapi;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.List;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Before;
import org.junit.Test;
import org.olat.basesecurity.BaseSecurity;
......@@ -199,20 +200,23 @@ public class ContactsTest extends OlatJerseyTestCase {
}
@Test
public void testGetContactsRest() throws IOException {
HttpClient c = loginWithCookie("rest-contacts-two", "A6B7C8");
public void testGetContactsRest() throws IOException, URISyntaxException {
RestConnection conn = new RestConnection();
assertTrue(conn.login("rest-contacts-two", "A6B7C8"));
UriBuilder uri = UriBuilder.fromUri(getContextURI()).path("contacts").queryParam("start", "0").queryParam("limit", "10");
GetMethod method = createGet(uri.build(), MediaType.APPLICATION_JSON, true);
int code = c.executeMethod(method);
assertEquals(code, 200);
InputStream body = method.getResponseBodyAsStream();
HttpGet method = conn.createGet(uri.build(), MediaType.APPLICATION_JSON, true);
HttpResponse response = conn.execute(method);
assertEquals(200, response.getStatusLine().getStatusCode());
InputStream body = response.getEntity().getContent();
UserVOes contacts = parse(body, UserVOes.class);
method.releaseConnection();
assertNotNull(contacts);
assertNotNull(contacts.getUsers());
assertEquals(1, contacts.getUsers().length);
assertEquals(1, contacts.getTotalCount());
//owner3 -> g4
assertEquals(part3.getKey(), contacts.getUsers()[0].getKey());
conn.shutdown();
}
}
......@@ -21,6 +21,7 @@ package org.olat.restapi;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
......@@ -39,11 +40,14 @@ import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonFactory;
......@@ -116,7 +120,7 @@ public class RestConnection {
return null;
}
public void addEntity(HttpPut put, NameValuePair... pairs)
public void addEntity(HttpEntityEnclosingRequestBase put, NameValuePair... pairs)
throws UnsupportedEncodingException {
if(pairs == null || pairs.length == 0) return;
......@@ -128,6 +132,27 @@ public class RestConnection {
put.setEntity(myEntity);
}
/**
* Add an object (application/json)
* @param put
* @param obj
* @throws UnsupportedEncodingException
*/
public void addJsonEntity(HttpEntityEnclosingRequestBase put, Object obj)
throws UnsupportedEncodingException {
if(obj == null) return;
String objectStr = stringuified(obj);
HttpEntity myEntity = new StringEntity(objectStr, MediaType.APPLICATION_JSON, "UTF-8");
put.setEntity(myEntity);
}
public HttpPut createPut(URI uri, String accept, boolean cookie) {
HttpPut put = new HttpPut(uri);
decorateHttpMessage(put,accept, "en", cookie);
return put;
}
public HttpPut createPut(URI uri, String accept, String langage, boolean cookie) {
HttpPut put = new HttpPut(uri);
decorateHttpMessage(put,accept, langage, cookie);
......@@ -146,6 +171,12 @@ public class RestConnection {
return get;
}
public HttpDelete createDelete(URI uri, String accept, boolean cookie) {
HttpDelete del = new HttpDelete(uri);
decorateHttpMessage(del, accept, "en", cookie);
return del;
}
private void decorateHttpMessage(HttpMessage msg, String accept, String langage, boolean cookie) {
if(cookie) {
HttpClientParams.setCookiePolicy(msg.getParams(), CookiePolicy.RFC_2109);
......@@ -179,6 +210,18 @@ public class RestConnection {
return getBaseURI().path(CONTEXT_PATH);
}
public String stringuified(Object obj) {
try {
ObjectMapper mapper = new ObjectMapper(jsonFactory);
StringWriter w = new StringWriter();
mapper.writeValue(w, obj);
return w.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public <U> U parse(HttpResponse response, Class<U> cl) {
try {
InputStream body = response.getEntity().getContent();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment