mirror of
https://github.com/sismics/docs.git
synced 2025-12-14 10:16:21 +00:00
#65: PUT /vocabulary resource
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
package com.sismics.docs.rest.resource;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObjectBuilder;
|
||||
import javax.ws.rs.FormParam;
|
||||
import javax.ws.rs.PUT;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
import com.sismics.docs.core.dao.jpa.VocabularyDao;
|
||||
import com.sismics.docs.core.model.jpa.Vocabulary;
|
||||
import com.sismics.rest.exception.ForbiddenClientException;
|
||||
import com.sismics.rest.util.ValidationUtil;
|
||||
|
||||
/**
|
||||
* Vocabulary REST resources.
|
||||
@@ -9,4 +19,41 @@ import javax.ws.rs.Path;
|
||||
*/
|
||||
@Path("/vocabulary")
|
||||
public class VocabularyResource extends BaseResource {
|
||||
/**
|
||||
* Add a vocabulary.
|
||||
*
|
||||
* @param name Name
|
||||
* @param value Value
|
||||
* @param order Order
|
||||
* @return Response
|
||||
*/
|
||||
@PUT
|
||||
public Response add(@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, false);
|
||||
value = ValidationUtil.validateLength(value, "value", 1, 100, false);
|
||||
Integer order = ValidationUtil.validateInteger(orderStr, "order");
|
||||
|
||||
// Create the comment
|
||||
VocabularyDao vocabularyDao = new VocabularyDao();
|
||||
Vocabulary vocabulary = new Vocabulary();
|
||||
vocabulary.setName(name);
|
||||
vocabulary.setValue(value);
|
||||
vocabulary.setOrder(order);
|
||||
vocabularyDao.create(vocabulary);
|
||||
|
||||
// Returns the comment
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
.add("id", vocabulary.getId())
|
||||
.add("name", vocabulary.getName())
|
||||
.add("value", vocabulary.getValue())
|
||||
.add("order", Integer.toString(vocabulary.getOrder()));
|
||||
return Response.ok().entity(response.build()).build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
package com.sismics.docs.rest;
|
||||
|
||||
import javax.json.JsonObject;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.sismics.util.filter.TokenBasedSecurityFilter;
|
||||
|
||||
/**
|
||||
* Exhaustive test of the vocabulary resource.
|
||||
*
|
||||
@@ -15,5 +22,18 @@ public class TestVocabularyResource extends BaseJerseyTest {
|
||||
*/
|
||||
@Test
|
||||
public void testVocabularyResource() throws Exception {
|
||||
// Login vocabulary1
|
||||
clientUtil.createUser("vocabulary1");
|
||||
String vocabulary1Token = clientUtil.login("vocabulary1");
|
||||
|
||||
// Create a vocabulary entry with vocabulary1
|
||||
JsonObject json = target().path("/vocabulary").request()
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, vocabulary1Token)
|
||||
.put(Entity.form(new Form()
|
||||
.param("name", "TEST_VOC_1")
|
||||
.param("value", "First value")
|
||||
.param("order", "0")), JsonObject.class);
|
||||
String vocabulary1Id = json.getString("id");
|
||||
Assert.assertNotNull(vocabulary1Id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user