2013-07-27 18:33:20 +02:00
|
|
|
package com.sismics.docs.rest;
|
|
|
|
|
2013-07-31 00:07:30 +02:00
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
2013-07-27 18:33:20 +02:00
|
|
|
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
|
|
|
|
2013-07-29 22:55:32 +02:00
|
|
|
// Create a tag
|
|
|
|
WebResource tagResource = resource().path("/tag");
|
|
|
|
tagResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
|
|
|
MultivaluedMapImpl postParams = new MultivaluedMapImpl();
|
|
|
|
postParams.add("name", "Super tag");
|
|
|
|
ClientResponse response = tagResource.put(ClientResponse.class, postParams);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
JSONObject json = response.getEntity(JSONObject.class);
|
|
|
|
String tag1Id = json.optString("id");
|
|
|
|
Assert.assertNotNull(tag1Id);
|
|
|
|
|
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-29 22:55:32 +02:00
|
|
|
postParams = new MultivaluedMapImpl();
|
2013-07-27 18:33:20 +02:00
|
|
|
postParams.add("title", "My super document 1");
|
|
|
|
postParams.add("description", "My super description for document 1");
|
2013-08-01 23:32:55 +02:00
|
|
|
postParams.add("tags", tag1Id);
|
2013-07-31 00:07:30 +02:00
|
|
|
long create1Date = new Date().getTime();
|
|
|
|
postParams.add("create_date", create1Date);
|
2013-07-29 22:55:32 +02:00
|
|
|
response = documentResource.put(ClientResponse.class, postParams);
|
2013-07-27 18:33:20 +02:00
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
2013-07-29 22:55:32 +02:00
|
|
|
json = response.getEntity(JSONObject.class);
|
2013-07-27 18:33:20 +02:00
|
|
|
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-31 00:07:30 +02:00
|
|
|
// Search documents by query
|
2013-07-29 00:04:34 +02:00
|
|
|
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"));
|
2013-07-31 00:07:30 +02:00
|
|
|
Assert.assertEquals(create1Date, documents.getJSONObject(0).getLong("create_date"));
|
|
|
|
|
|
|
|
// Search documents by date
|
|
|
|
documentResource = resource().path("/document/list");
|
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
|
|
|
getParams = new MultivaluedMapImpl();
|
|
|
|
getParams.putSingle("create_date_min", create1Date - 3600000);
|
|
|
|
getParams.putSingle("create_date_max", create1Date + 1800000);
|
|
|
|
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"));
|
2013-07-31 20:57:12 +02:00
|
|
|
|
|
|
|
// Search documents by tag
|
|
|
|
documentResource = resource().path("/document/list");
|
|
|
|
documentResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
|
|
|
getParams = new MultivaluedMapImpl();
|
2013-08-01 23:32:55 +02:00
|
|
|
getParams.putSingle("tags", tag1Id);
|
2013-07-31 20:57:12 +02:00
|
|
|
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"));
|
2013-07-29 00:04:34 +02:00
|
|
|
|
|
|
|
// 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"));
|
2013-07-29 22:55:32 +02:00
|
|
|
JSONArray tags = json.getJSONArray("tags");
|
|
|
|
Assert.assertEquals(1, tags.length());
|
|
|
|
Assert.assertEquals(tag1Id, tags.getJSONObject(0).getString("id"));
|
|
|
|
|
|
|
|
// Create a tag
|
|
|
|
tagResource = resource().path("/tag");
|
|
|
|
tagResource.addFilter(new CookieAuthenticationFilter(document1Token));
|
|
|
|
postParams = new MultivaluedMapImpl();
|
|
|
|
postParams.add("name", "Super tag 2");
|
|
|
|
response = tagResource.put(ClientResponse.class, postParams);
|
|
|
|
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
|
|
|
|
json = response.getEntity(JSONObject.class);
|
|
|
|
String tag2Id = json.optString("id");
|
|
|
|
Assert.assertNotNull(tag1Id);
|
2013-07-27 18:33:20 +02:00
|
|
|
|
|
|
|
// 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");
|
2013-08-01 23:32:55 +02:00
|
|
|
postParams.add("tags", tag2Id);
|
2013-07-27 18:33:20 +02:00
|
|
|
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"));
|
2013-07-29 22:55:32 +02:00
|
|
|
tags = json.getJSONArray("tags");
|
|
|
|
Assert.assertEquals(1, tags.length());
|
|
|
|
Assert.assertEquals(tag2Id, tags.getJSONObject(0).getString("id"));
|
2013-07-27 18:33:20 +02:00
|
|
|
|
|
|
|
// 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()));
|
|
|
|
}
|
|
|
|
}
|