1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 01:36:18 +00:00

Tag stats (server)

This commit is contained in:
jendib
2013-08-04 14:19:37 +02:00
parent f5871e89e2
commit 97fa6b10f7
5 changed files with 143 additions and 7 deletions

View File

@@ -19,6 +19,7 @@ import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import com.sismics.docs.core.dao.jpa.TagDao;
import com.sismics.docs.core.dao.jpa.dto.TagStatDto;
import com.sismics.docs.core.model.jpa.Tag;
import com.sismics.rest.exception.ClientException;
import com.sismics.rest.exception.ForbiddenClientException;
@@ -59,6 +60,35 @@ public class TagResource extends BaseResource {
return Response.ok().entity(response).build();
}
/**
* Returns stats on tags.
*
* @return Response
* @throws JSONException
*/
@GET
@Path("/stats")
@Produces(MediaType.APPLICATION_JSON)
public Response stats() throws JSONException {
if (!authenticate()) {
throw new ForbiddenClientException();
}
TagDao tagDao = new TagDao();
List<TagStatDto> tagStatDtoList = tagDao.getStats(principal.getId());
JSONObject response = new JSONObject();
List<JSONObject> items = new ArrayList<JSONObject>();
for (TagStatDto tagStatDto : tagStatDtoList) {
JSONObject item = new JSONObject();
item.put("id", tagStatDto.getId());
item.put("name", tagStatDto.getName());
item.put("count", tagStatDto.getCount());
items.add(item);
}
response.put("stats", items);
return Response.ok().entity(response).build();
}
/**
* Creates a new tag.
*

View File

@@ -7,7 +7,6 @@ import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.Test;
import com.sismics.docs.rest.BaseJerseyTest;
import com.sismics.docs.rest.filter.CookieAuthenticationFilter;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.ClientResponse.Status;
@@ -42,6 +41,48 @@ public class TestTagResource extends BaseJerseyTest {
String tag3Id = json.optString("id");
Assert.assertNotNull(tag3Id);
// Create a tag
tagResource = resource().path("/tag");
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
postParams = new MultivaluedMapImpl();
postParams.add("name", "Tag 4");
response = tagResource.put(ClientResponse.class, postParams);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
String tag4Id = json.optString("id");
Assert.assertNotNull(tag4Id);
// Create a document
WebResource documentResource = resource().path("/document");
documentResource.addFilter(new CookieAuthenticationFilter(tag1Token));
postParams = new MultivaluedMapImpl();
postParams.add("title", "My super document 1");
postParams.add("tags", tag3Id);
response = documentResource.put(ClientResponse.class, postParams);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
// Create a document
documentResource = resource().path("/document");
documentResource.addFilter(new CookieAuthenticationFilter(tag1Token));
postParams = new MultivaluedMapImpl();
postParams.add("title", "My super document 1");
postParams.add("tags", tag4Id);
response = documentResource.put(ClientResponse.class, postParams);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
// Get tag stats
tagResource = resource().path("/tag/stats");
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
response = tagResource.get(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
JSONArray stats = json.getJSONArray("stats");
Assert.assertTrue(stats.length() == 2);
Assert.assertEquals(1, stats.getJSONObject(0).getInt("count"));
Assert.assertEquals(1, stats.getJSONObject(1).getInt("count"));
// Get all tags
tagResource = resource().path("/tag/list");
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
@@ -51,15 +92,15 @@ public class TestTagResource extends BaseJerseyTest {
JSONArray tags = json.getJSONArray("tags");
Assert.assertTrue(tags.length() > 0);
// Update a document
tagResource = resource().path("/tag/" + tag3Id);
// Update a tag
tagResource = resource().path("/tag/" + tag4Id);
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
postParams = new MultivaluedMapImpl();
postParams.add("name", "Updated name");
response = tagResource.post(ClientResponse.class, postParams);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
Assert.assertEquals(tag3Id, json.getString("id"));
Assert.assertEquals(tag4Id, json.getString("id"));
// Get all tags
tagResource = resource().path("/tag/list");
@@ -72,7 +113,7 @@ public class TestTagResource extends BaseJerseyTest {
Assert.assertEquals("Updated name", tags.getJSONObject(0).getString("name"));
// Deletes a tag
tagResource = resource().path("/tag/" + tag3Id);
tagResource = resource().path("/tag/" + tag4Id);
tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));
response = tagResource.delete(ClientResponse.class);
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
@@ -86,6 +127,6 @@ public class TestTagResource extends BaseJerseyTest {
Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus()));
json = response.getEntity(JSONObject.class);
tags = json.getJSONArray("tags");
Assert.assertTrue(tags.length() == 0);
Assert.assertTrue(tags.length() == 1);
}
}