2013-07-27 18:33:20 +02:00
|
|
|
package com.sismics.docs.rest;
|
|
|
|
|
|
|
|
import junit.framework.Assert;
|
|
|
|
|
|
|
|
import org.codehaus.jettison.json.JSONArray;
|
|
|
|
import org.codehaus.jettison.json.JSONException;
|
|
|
|
import org.codehaus.jettison.json.JSONObject;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
|
|
|
|
import com.sun.jersey.api.client.ClientResponse;
|
|
|
|
import com.sun.jersey.api.client.ClientResponse.Status;
|
|
|
|
import com.sun.jersey.api.client.WebResource;
|
|
|
|
import com.sun.jersey.core.util.MultivaluedMapImpl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exhaustive test of the document resource.
|
|
|
|
*
|
|
|
|
* @author bgamard
|
|
|
|
*/
|
|
|
|
public class TestDocumentResource extends BaseJerseyTest {
|
|
|
|
/**
|
|
|
|
* Test the document resource.
|
|
|
|
*
|
|
|
|
* @throws JSONException
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testDocumentResource() throws JSONException {
|
2013-07-28 02:06:00 +02:00
|
|
|
// Login document1
|
|
|
|
clientUtil.createUser("document1");
|
|
|
|
String document1Token = clientUtil.login("document1");
|
2013-07-27 18:33:20 +02:00
|
|
|
|
|
|
|
// Create a document
|
|
|
|
WebResource documentResource = resource().path("/document");
|
2013-07-28 02:06:00 +02:00
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
2013-07-27 18:33:20 +02:00
|
|
|
MultivaluedMapImpl postParams = new MultivaluedMapImpl();
|
|
|
|
postParams.add("title", "My super document 1");
|
|
|
|
postParams.add("description", "My super description for document 1");
|
|
|
|
ClientResponse response = documentResource.put(ClientResponse.class, postParams);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
JSONObject json = response.getEntity(JSONObject.class);
|
|
|
|
String document1Id = json.optString("id");
|
|
|
|
Assert.assertNotNull(document1Id);
|
|
|
|
|
|
|
|
// List all documents
|
|
|
|
documentResource = resource().path("/document/list");
|
2013-07-28 02:06:00 +02:00
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
2013-07-27 18:33:20 +02:00
|
|
|
MultivaluedMapImpl getParams = new MultivaluedMapImpl();
|
|
|
|
getParams.putSingle("sort_column", 3);
|
|
|
|
getParams.putSingle("asc", false);
|
|
|
|
response = documentResource.queryParams(getParams).get(ClientResponse.class);
|
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
JSONArray documents = json.getJSONArray("documents");
|
|
|
|
Assert.assertTrue(documents.length() == 1);
|
|
|
|
Assert.assertEquals(document1Id, documents.getJSONObject(0).getString("id"));
|
|
|
|
|
2013-07-29 00:04:34 +02:00
|
|
|
// Search documents
|
|
|
|
documentResource = resource().path("/document/list");
|
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
|
|
|
getParams = new MultivaluedMapImpl();
|
|
|
|
getParams.putSingle("search", "Sup");
|
|
|
|
response = documentResource.queryParams(getParams).get(ClientResponse.class);
|
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
documents = json.getJSONArray("documents");
|
|
|
|
Assert.assertTrue(documents.length() == 1);
|
|
|
|
Assert.assertEquals(document1Id, documents.getJSONObject(0).getString("id"));
|
|
|
|
|
|
|
|
// Search documents (nothing)
|
|
|
|
documentResource = resource().path("/document/list");
|
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
|
|
|
getParams = new MultivaluedMapImpl();
|
|
|
|
getParams.putSingle("search", "random");
|
|
|
|
response = documentResource.queryParams(getParams).get(ClientResponse.class);
|
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
documents = json.getJSONArray("documents");
|
|
|
|
Assert.assertTrue(documents.length() == 0);
|
|
|
|
|
2013-07-27 18:33:20 +02:00
|
|
|
// Get a document
|
2013-07-28 01:07:04 +02:00
|
|
|
documentResource = resource().path("/document/" + document1Id);
|
2013-07-28 02:06:00 +02:00
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
2013-07-28 01:07:04 +02:00
|
|
|
response = documentResource.get(ClientResponse.class);
|
2013-07-27 18:33:20 +02:00
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
Assert.assertEquals(document1Id, json.getString("id"));
|
|
|
|
|
|
|
|
// Update a document
|
|
|
|
documentResource = resource().path("/document/" + document1Id);
|
2013-07-28 02:06:00 +02:00
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
2013-07-27 18:33:20 +02:00
|
|
|
postParams = new MultivaluedMapImpl();
|
|
|
|
postParams.add("title", "My new super document 1");
|
|
|
|
postParams.add("description", "My new super description for document 1");
|
|
|
|
response = documentResource.post(ClientResponse.class, postParams);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
json = response.getEntity(JSONObject.class);
|
2013-07-28 01:58:22 +02:00
|
|
|
Assert.assertEquals(document1Id, json.getString("id"));
|
2013-07-27 18:33:20 +02:00
|
|
|
|
|
|
|
// Get a document
|
2013-07-28 01:07:04 +02:00
|
|
|
documentResource = resource().path("/document/" + document1Id);
|
2013-07-28 02:06:00 +02:00
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
2013-07-28 01:07:04 +02:00
|
|
|
response = documentResource.get(ClientResponse.class);
|
2013-07-27 18:33:20 +02:00
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
Assert.assertTrue(json.getString("title").contains("new"));
|
|
|
|
Assert.assertTrue(json.getString("description").contains("new"));
|
|
|
|
|
|
|
|
// Deletes a document
|
|
|
|
documentResource = resource().path("/document/" + document1Id);
|
2013-07-28 02:06:00 +02:00
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
2013-07-27 18:33:20 +02:00
|
|
|
response = documentResource.delete(ClientResponse.class);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
Assert.assertEquals("ok", json.getString("status"));
|
|
|
|
|
|
|
|
// Get a document (KO)
|
2013-07-28 01:07:04 +02:00
|
|
|
documentResource = resource().path("/document/" + document1Id);
|
2013-07-28 02:06:00 +02:00
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
2013-07-28 01:07:04 +02:00
|
|
|
response = documentResource.get(ClientResponse.class);
|
2013-07-27 18:33:20 +02:00
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
Assert.assertEquals(Status.BAD_REQUEST, Status.fromStatusCode(response.getStatus()));
|
|
|
|
}
|
|
|
|
}
|