mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 17:56:20 +00:00
#65: Vocabulary resource
This commit is contained in:
@@ -86,9 +86,6 @@ public class CommentResource extends BaseResource {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
// Validate input data
|
||||
ValidationUtil.validateRequired(id, "id");
|
||||
|
||||
// Get the comment
|
||||
CommentDao commentDao = new CommentDao();
|
||||
Comment comment = commentDao.getActiveById(id);
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
package com.sismics.docs.rest.resource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonArrayBuilder;
|
||||
import javax.json.JsonObjectBuilder;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import com.sismics.docs.core.dao.jpa.VocabularyDao;
|
||||
import com.sismics.docs.core.model.jpa.Vocabulary;
|
||||
@@ -19,6 +27,30 @@ import com.sismics.rest.util.ValidationUtil;
|
||||
*/
|
||||
@Path("/vocabulary")
|
||||
public class VocabularyResource extends BaseResource {
|
||||
@GET
|
||||
@Path("{name: [a-z0-9\\-]+}")
|
||||
public Response get(@PathParam("name") String name) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
// Assemble results
|
||||
VocabularyDao vocabularyDao = new VocabularyDao();
|
||||
List<Vocabulary> vocabularyList = vocabularyDao.getByName(name);
|
||||
JsonArrayBuilder entries = Json.createArrayBuilder();
|
||||
for (Vocabulary vocabulary : vocabularyList) {
|
||||
entries.add(Json.createObjectBuilder()
|
||||
.add("id", vocabulary.getId())
|
||||
.add("value", vocabulary.getValue())
|
||||
.add("order", vocabulary.getOrder()));
|
||||
}
|
||||
|
||||
// Always return OK
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("entries", entries);
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a vocabulary.
|
||||
*
|
||||
@@ -37,10 +69,11 @@ public class VocabularyResource extends BaseResource {
|
||||
|
||||
// Validate input data
|
||||
name = ValidationUtil.validateLength(name, "name", 1, 50, false);
|
||||
ValidationUtil.validateRegex(name, "name", "[a-z0-9\\-]+");
|
||||
value = ValidationUtil.validateLength(value, "value", 1, 100, false);
|
||||
Integer order = ValidationUtil.validateInteger(orderStr, "order");
|
||||
|
||||
// Create the comment
|
||||
// Create the vocabulary
|
||||
VocabularyDao vocabularyDao = new VocabularyDao();
|
||||
Vocabulary vocabulary = new Vocabulary();
|
||||
vocabulary.setName(name);
|
||||
@@ -48,12 +81,96 @@ public class VocabularyResource extends BaseResource {
|
||||
vocabulary.setOrder(order);
|
||||
vocabularyDao.create(vocabulary);
|
||||
|
||||
// Returns the comment
|
||||
// Returns the vocabulary
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("id", vocabulary.getId())
|
||||
.add("name", vocabulary.getName())
|
||||
.add("value", vocabulary.getValue())
|
||||
.add("order", Integer.toString(vocabulary.getOrder()));
|
||||
.add("order", vocabulary.getOrder());
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a vocabulary.
|
||||
*
|
||||
* @param name Name
|
||||
* @param value Value
|
||||
* @param order Order
|
||||
* @return Response
|
||||
*/
|
||||
@POST
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
public Response update(@PathParam("id") String id,
|
||||
@FormParam("name") String name,
|
||||
@FormParam("value") String value,
|
||||
@FormParam("order") String orderStr) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
// Validate input data
|
||||
name = ValidationUtil.validateLength(name, "name", 1, 50, true);
|
||||
ValidationUtil.validateRegex(name, "name", "[a-z0-9\\-]+");
|
||||
value = ValidationUtil.validateLength(value, "value", 1, 100, true);
|
||||
Integer order = null;
|
||||
if (orderStr != null) {
|
||||
order = ValidationUtil.validateInteger(orderStr, "order");
|
||||
}
|
||||
|
||||
// Get the vocabulary entry
|
||||
VocabularyDao vocabularyDao = new VocabularyDao();
|
||||
Vocabulary vocabulary = vocabularyDao.getById(id);
|
||||
if (vocabulary == null) {
|
||||
return Response.status(Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
// Update the vocabulary
|
||||
if (name != null) {
|
||||
vocabulary.setName(name);
|
||||
}
|
||||
if (value != null) {
|
||||
vocabulary.setValue(value);
|
||||
}
|
||||
if (order != null) {
|
||||
vocabulary.setOrder(order);
|
||||
}
|
||||
vocabularyDao.update(vocabulary);
|
||||
|
||||
// Returns the vocabulary
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("id", vocabulary.getId())
|
||||
.add("name", vocabulary.getName())
|
||||
.add("value", vocabulary.getValue())
|
||||
.add("order", vocabulary.getOrder());
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a vocabulary.
|
||||
*
|
||||
* @param id ID
|
||||
* @return Response
|
||||
*/
|
||||
@DELETE
|
||||
@Path("{id: [a-z0-9\\-]+}")
|
||||
public Response delete(@PathParam("id") String id) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
|
||||
// Get the vocabulary
|
||||
VocabularyDao vocabularyDao = new VocabularyDao();
|
||||
Vocabulary vocabulary = vocabularyDao.getById(id);
|
||||
if (vocabulary == null) {
|
||||
return Response.status(Status.NOT_FOUND).build();
|
||||
}
|
||||
|
||||
// Delete the vocabulary
|
||||
vocabularyDao.delete(id);
|
||||
|
||||
// Always return OK
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("status", "ok");
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.sismics.docs.rest;
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@@ -26,14 +28,83 @@ public class TestVocabularyResource extends BaseJerseyTest {
|
||||
clientUtil.createUser("vocabulary1");
|
||||
String vocabulary1Token = clientUtil.login("vocabulary1");
|
||||
|
||||
// Get coverage vocabularies entries
|
||||
JsonObject json = target().path("/vocabulary/coverage").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.get(JsonObject.class);
|
||||
Assert.assertEquals(249, json.getJsonArray("entries").size());
|
||||
JsonObject entry = json.getJsonArray("entries").getJsonObject(0);
|
||||
Assert.assertEquals("coverage-afg", entry.getString("id"));
|
||||
Assert.assertEquals("Afghanistan", entry.getString("value"));
|
||||
Assert.assertEquals(0, entry.getJsonNumber("order").intValue());
|
||||
entry = json.getJsonArray("entries").getJsonObject(248);
|
||||
Assert.assertEquals("coverage-zwe", entry.getString("id"));
|
||||
Assert.assertEquals("Zimbabwe", entry.getString("value"));
|
||||
Assert.assertEquals(248, entry.getJsonNumber("order").intValue());
|
||||
|
||||
// Create a vocabulary entry with vocabulary1
|
||||
JsonObject json = target().path("/vocabulary").request()
|
||||
json = target().path("/vocabulary").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.put(Entity.form(new Form()
|
||||
.param("name", "TEST_VOC_1")
|
||||
.param("name", "test-voc-1")
|
||||
.param("value", "First value")
|
||||
.param("order", "0")), JsonObject.class);
|
||||
String vocabulary1Id = json.getString("id");
|
||||
Assert.assertNotNull(vocabulary1Id);
|
||||
Assert.assertEquals("test-voc-1", json.getString("name"));
|
||||
Assert.assertEquals("First value", json.getString("value"));
|
||||
Assert.assertEquals(0, json.getJsonNumber("order").intValue());
|
||||
|
||||
// Create a vocabulary entry with vocabulary1
|
||||
Response response = target().path("/vocabulary").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.put(Entity.form(new Form()
|
||||
.param("name", "NOT_VALID")
|
||||
.param("value", "First value")
|
||||
.param("order", "0")));
|
||||
Assert.assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
|
||||
|
||||
// Get test-voc-1 vocabularies entries
|
||||
json = target().path("/vocabulary/test-voc-1").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.get(JsonObject.class);
|
||||
Assert.assertEquals(1, json.getJsonArray("entries").size());
|
||||
entry = json.getJsonArray("entries").getJsonObject(0);
|
||||
Assert.assertEquals(vocabulary1Id, entry.getString("id"));
|
||||
Assert.assertEquals("First value", entry.getString("value"));
|
||||
Assert.assertEquals(0, entry.getJsonNumber("order").intValue());
|
||||
|
||||
// Update a vocabulary entry with vocabulary1
|
||||
json = target().path("/vocabulary/" + vocabulary1Id).request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.post(Entity.form(new Form()
|
||||
.param("name", "test-voc-1-updated")
|
||||
.param("value", "First value updated")
|
||||
.param("order", "1")), JsonObject.class);
|
||||
Assert.assertEquals(vocabulary1Id, json.getString("id"));
|
||||
Assert.assertEquals("test-voc-1-updated", json.getString("name"));
|
||||
Assert.assertEquals("First value updated", json.getString("value"));
|
||||
Assert.assertEquals(1, json.getJsonNumber("order").intValue());
|
||||
|
||||
// Get test-voc-1-updated vocabularies entries
|
||||
json = target().path("/vocabulary/test-voc-1-updated").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.get(JsonObject.class);
|
||||
Assert.assertEquals(1, json.getJsonArray("entries").size());
|
||||
entry = json.getJsonArray("entries").getJsonObject(0);
|
||||
Assert.assertEquals(vocabulary1Id, entry.getString("id"));
|
||||
Assert.assertEquals("First value updated", entry.getString("value"));
|
||||
Assert.assertEquals(1, entry.getJsonNumber("order").intValue());
|
||||
|
||||
// Delete a vocabulary entry with vocabulary1
|
||||
json = target().path("/vocabulary/" + vocabulary1Id).request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.delete(JsonObject.class);
|
||||
|
||||
// Get test-voc-1-updated vocabularies entries
|
||||
json = target().path("/vocabulary/test-voc-1-updated").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.get(JsonObject.class);
|
||||
Assert.assertEquals(0, json.getJsonArray("entries").size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user