From f8dc08b02b451e165dad7e4bdca2b4670c09a374 Mon Sep 17 00:00:00 2001 From: Benjamin Gamard Date: Wed, 15 May 2019 13:34:01 +0200 Subject: [PATCH] #300: custom metadata fields: API admin --- docs-android/app/build.gradle | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 4 +- .../docs/core/constant/MetadataType.java | 14 ++ .../sismics/docs/core/dao/MetadataDao.java | 148 +++++++++++++ .../sismics/docs/core/dao/RouteModelDao.java | 3 +- .../core/dao/criteria/MetadataCriteria.java | 9 + .../docs/core/dao/dto/MetadataDto.java | 52 +++++ .../sismics/docs/core/model/jpa/Metadata.java | 92 ++++++++ .../src/main/resources/config.properties | 2 +- .../resources/db/update/dbupdate-024-0.sql | 3 + docs-web/src/dev/resources/config.properties | 2 +- .../docs/rest/resource/MetadataResource.java | 208 ++++++++++++++++++ .../rest/resource/VocabularyResource.java | 2 +- docs-web/src/prod/resources/config.properties | 2 +- .../src/stress/resources/config.properties | 2 +- .../docs/rest/TestMetadataResource.java | 82 +++++++ .../docs/rest/TestVocabularyResource.java | 11 +- 17 files changed, 622 insertions(+), 16 deletions(-) create mode 100644 docs-core/src/main/java/com/sismics/docs/core/constant/MetadataType.java create mode 100644 docs-core/src/main/java/com/sismics/docs/core/dao/MetadataDao.java create mode 100644 docs-core/src/main/java/com/sismics/docs/core/dao/criteria/MetadataCriteria.java create mode 100644 docs-core/src/main/java/com/sismics/docs/core/dao/dto/MetadataDto.java create mode 100644 docs-core/src/main/java/com/sismics/docs/core/model/jpa/Metadata.java create mode 100644 docs-core/src/main/resources/db/update/dbupdate-024-0.sql create mode 100644 docs-web/src/main/java/com/sismics/docs/rest/resource/MetadataResource.java create mode 100644 docs-web/src/test/java/com/sismics/docs/rest/TestMetadataResource.java diff --git a/docs-android/app/build.gradle b/docs-android/app/build.gradle index a6238e78..1e5664eb 100644 --- a/docs-android/app/build.gradle +++ b/docs-android/app/build.gradle @@ -4,7 +4,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.3.0' + classpath 'com.android.tools.build:gradle:3.4.0' } } apply plugin: 'com.android.application' diff --git a/docs-android/gradle/wrapper/gradle-wrapper.properties b/docs-android/gradle/wrapper/gradle-wrapper.properties index b0546596..b3b5f4b7 100644 --- a/docs-android/gradle/wrapper/gradle-wrapper.properties +++ b/docs-android/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Jan 30 16:31:31 CET 2019 +#Tue May 07 11:49:13 CEST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip diff --git a/docs-core/src/main/java/com/sismics/docs/core/constant/MetadataType.java b/docs-core/src/main/java/com/sismics/docs/core/constant/MetadataType.java new file mode 100644 index 00000000..5a7ef12e --- /dev/null +++ b/docs-core/src/main/java/com/sismics/docs/core/constant/MetadataType.java @@ -0,0 +1,14 @@ +package com.sismics.docs.core.constant; + +/** + * Metadata type. + * + * @author bgamard + */ +public enum MetadataType { + STRING, + INTEGER, + FLOAT, + DATE, + BOOLEAN +} diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/MetadataDao.java b/docs-core/src/main/java/com/sismics/docs/core/dao/MetadataDao.java new file mode 100644 index 00000000..7878aa8e --- /dev/null +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/MetadataDao.java @@ -0,0 +1,148 @@ +package com.sismics.docs.core.dao; + +import com.google.common.base.Joiner; +import com.sismics.docs.core.constant.AuditLogType; +import com.sismics.docs.core.constant.MetadataType; +import com.sismics.docs.core.dao.criteria.MetadataCriteria; +import com.sismics.docs.core.dao.dto.MetadataDto; +import com.sismics.docs.core.model.jpa.Metadata; +import com.sismics.docs.core.util.AuditLogUtil; +import com.sismics.docs.core.util.jpa.QueryParam; +import com.sismics.docs.core.util.jpa.QueryUtil; +import com.sismics.docs.core.util.jpa.SortCriteria; +import com.sismics.util.context.ThreadLocalContext; + +import javax.persistence.EntityManager; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import java.util.*; + +/** + * Metadata DAO. + * + * @author bgamard + */ +public class MetadataDao { + /** + * Creates a new metdata. + * + * @param metadata Metadata + * @param userId User ID + * @return New ID + */ + public String create(Metadata metadata, String userId) { + // Create the UUID + metadata.setId(UUID.randomUUID().toString()); + + // Create the metadata + EntityManager em = ThreadLocalContext.get().getEntityManager(); + em.persist(metadata); + + // Create audit log + AuditLogUtil.create(metadata, AuditLogType.CREATE, userId); + + return metadata.getId(); + } + + /** + * Update a metadata. + * + * @param metadata Metadata to update + * @param userId User ID + * @return Updated metadata + */ + public Metadata update(Metadata metadata, String userId) { + EntityManager em = ThreadLocalContext.get().getEntityManager(); + + // Get the metadata + Query q = em.createQuery("select r from Metadata r where r.id = :id and r.deleteDate is null"); + q.setParameter("id", metadata.getId()); + Metadata metadataDb = (Metadata) q.getSingleResult(); + + // Update the metadata + metadataDb.setName(metadata.getName()); + + // Create audit log + AuditLogUtil.create(metadataDb, AuditLogType.UPDATE, userId); + + return metadataDb; + } + + /** + * Gets an active metadata by its ID. + * + * @param id Metadata ID + * @return Metadata + */ + public Metadata getActiveById(String id) { + EntityManager em = ThreadLocalContext.get().getEntityManager(); + try { + Query q = em.createQuery("select r from Metadata r where r.id = :id and r.deleteDate is null"); + q.setParameter("id", id); + return (Metadata) q.getSingleResult(); + } catch (NoResultException e) { + return null; + } + } + + /** + * Deletes a metadata. + * + * @param id Metadata ID + * @param userId User ID + */ + public void delete(String id, String userId) { + EntityManager em = ThreadLocalContext.get().getEntityManager(); + + // Get the metadata + Query q = em.createQuery("select r from Metadata r where r.id = :id and r.deleteDate is null"); + q.setParameter("id", id); + Metadata metadataDb = (Metadata) q.getSingleResult(); + + // Delete the metadata + Date dateNow = new Date(); + metadataDb.setDeleteDate(dateNow); + + // Create audit log + AuditLogUtil.create(metadataDb, AuditLogType.DELETE, userId); + } + + /** + * Returns the list of all metadata. + * + * @param criteria Search criteria + * @param sortCriteria Sort criteria + * @return List of metadata + */ + public List findByCriteria(MetadataCriteria criteria, SortCriteria sortCriteria) { + Map parameterMap = new HashMap<>(); + List criteriaList = new ArrayList<>(); + + StringBuilder sb = new StringBuilder("select m.MET_ID_C c0, m.MET_NAME_C c1, m.MET_TYPE_C c2"); + sb.append(" from T_METADATA m "); + + criteriaList.add("m.MET_DELETEDATE_D is null"); + + if (!criteriaList.isEmpty()) { + sb.append(" where "); + sb.append(Joiner.on(" and ").join(criteriaList)); + } + + // Perform the search + QueryParam queryParam = QueryUtil.getSortedQueryParam(new QueryParam(sb.toString(), parameterMap), sortCriteria); + @SuppressWarnings("unchecked") + List l = QueryUtil.getNativeQuery(queryParam).getResultList(); + + // Assemble results + List dtoList = new ArrayList<>(); + for (Object[] o : l) { + int i = 0; + MetadataDto dto = new MetadataDto(); + dto.setId((String) o[i++]); + dto.setName((String) o[i++]); + dto.setType(MetadataType.valueOf((String) o[i])); + dtoList.add(dto); + } + return dtoList; + } +} diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/RouteModelDao.java b/docs-core/src/main/java/com/sismics/docs/core/dao/RouteModelDao.java index 63952aaa..f104beb1 100644 --- a/docs-core/src/main/java/com/sismics/docs/core/dao/RouteModelDao.java +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/RouteModelDao.java @@ -4,7 +4,6 @@ import com.google.common.base.Joiner; import com.sismics.docs.core.constant.AuditLogType; import com.sismics.docs.core.dao.criteria.RouteModelCriteria; import com.sismics.docs.core.dao.dto.RouteModelDto; -import com.sismics.docs.core.model.jpa.File; import com.sismics.docs.core.model.jpa.RouteModel; import com.sismics.docs.core.util.AuditLogUtil; import com.sismics.docs.core.util.SecurityUtil; @@ -62,7 +61,7 @@ public class RouteModelDao { q.setParameter("id", routeModel.getId()); RouteModel routeModelDb = (RouteModel) q.getSingleResult(); - // Update the group + // Update the route model routeModelDb.setName(routeModel.getName()); routeModelDb.setSteps(routeModel.getSteps()); diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/criteria/MetadataCriteria.java b/docs-core/src/main/java/com/sismics/docs/core/dao/criteria/MetadataCriteria.java new file mode 100644 index 00000000..5717df09 --- /dev/null +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/criteria/MetadataCriteria.java @@ -0,0 +1,9 @@ +package com.sismics.docs.core.dao.criteria; + +/** + * Metadata criteria. + * + * @author bgamard + */ +public class MetadataCriteria { +} diff --git a/docs-core/src/main/java/com/sismics/docs/core/dao/dto/MetadataDto.java b/docs-core/src/main/java/com/sismics/docs/core/dao/dto/MetadataDto.java new file mode 100644 index 00000000..b5cefaa1 --- /dev/null +++ b/docs-core/src/main/java/com/sismics/docs/core/dao/dto/MetadataDto.java @@ -0,0 +1,52 @@ +package com.sismics.docs.core.dao.dto; + +import com.sismics.docs.core.constant.MetadataType; + +/** + * Metadata DTO. + * + * @author bgamard + */ +public class MetadataDto { + /** + * Metadata ID. + */ + private String id; + + /** + * Name. + */ + private String name; + + /** + * Type. + */ + private MetadataType type; + + public String getId() { + return id; + } + + public MetadataDto setId(String id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public MetadataDto setName(String name) { + this.name = name; + return this; + } + + public MetadataType getType() { + return type; + } + + public MetadataDto setType(MetadataType type) { + this.type = type; + return this; + } +} diff --git a/docs-core/src/main/java/com/sismics/docs/core/model/jpa/Metadata.java b/docs-core/src/main/java/com/sismics/docs/core/model/jpa/Metadata.java new file mode 100644 index 00000000..8c64ec2d --- /dev/null +++ b/docs-core/src/main/java/com/sismics/docs/core/model/jpa/Metadata.java @@ -0,0 +1,92 @@ +package com.sismics.docs.core.model.jpa; + +import com.google.common.base.MoreObjects; +import com.sismics.docs.core.constant.MetadataType; + +import javax.persistence.*; +import java.util.Date; + +/** + * Metadata entity. + * + * @author bgamard + */ +@Entity +@Table(name = "T_METADATA") +public class Metadata implements Loggable { + /** + * Metadata ID. + */ + @Id + @Column(name = "MET_ID_C", length = 36) + private String id; + + /** + * Name. + */ + @Column(name = "MET_NAME_C", length = 50, nullable = false) + private String name; + + /** + * Type. + */ + @Column(name = "MET_TYPE_C", length = 20, nullable = false) + @Enumerated(EnumType.STRING) + private MetadataType type; + + /** + * Deletion date. + */ + @Column(name = "MET_DELETEDATE_D") + private Date deleteDate; + + public String getId() { + return id; + } + + public Metadata setId(String id) { + this.id = id; + return this; + } + + public String getName() { + return name; + } + + public Metadata setName(String name) { + this.name = name; + return this; + } + + public MetadataType getType() { + return type; + } + + public Metadata setType(MetadataType type) { + this.type = type; + return this; + } + + @Override + public Date getDeleteDate() { + return deleteDate; + } + + public void setDeleteDate(Date deleteDate) { + this.deleteDate = deleteDate; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("id", id) + .add("name", name) + .add("type", type) + .toString(); + } + + @Override + public String toMessage() { + return name; + } +} diff --git a/docs-core/src/main/resources/config.properties b/docs-core/src/main/resources/config.properties index 0ac161b7..a2ce72d5 100644 --- a/docs-core/src/main/resources/config.properties +++ b/docs-core/src/main/resources/config.properties @@ -1 +1 @@ -db.version=23 \ No newline at end of file +db.version=24 \ No newline at end of file diff --git a/docs-core/src/main/resources/db/update/dbupdate-024-0.sql b/docs-core/src/main/resources/db/update/dbupdate-024-0.sql new file mode 100644 index 00000000..7ed6573b --- /dev/null +++ b/docs-core/src/main/resources/db/update/dbupdate-024-0.sql @@ -0,0 +1,3 @@ +create cached table T_METADATA ( MET_ID_C varchar(36) not null, MET_NAME_C varchar(50) not null, MET_TYPE_C varchar(20) not null, MET_DELETEDATE_D datetime, primary key (MET_ID_C) ); +create cached table T_DOCUMENT_METADATA ( DME_ID_C varchar(36) not null, DME_IDDOCUMENT_C varchar(36) not null, DME_IDMETADATA_C varchar(36) not null, DME_VALUE_C varchar(4000) not null, DME_DELETEDATE_D datetime, primary key (DME_ID_C) ); +update T_CONFIG set CFG_VALUE_C = '24' where CFG_ID_C = 'DB_VERSION'; diff --git a/docs-web/src/dev/resources/config.properties b/docs-web/src/dev/resources/config.properties index 8e983362..9dd002b9 100644 --- a/docs-web/src/dev/resources/config.properties +++ b/docs-web/src/dev/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=23 \ No newline at end of file +db.version=24 \ No newline at end of file diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/MetadataResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/MetadataResource.java new file mode 100644 index 00000000..5dac5f54 --- /dev/null +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/MetadataResource.java @@ -0,0 +1,208 @@ +package com.sismics.docs.rest.resource; + +import com.sismics.docs.core.constant.MetadataType; +import com.sismics.docs.core.dao.MetadataDao; +import com.sismics.docs.core.dao.criteria.MetadataCriteria; +import com.sismics.docs.core.dao.dto.MetadataDto; +import com.sismics.docs.core.model.jpa.Metadata; +import com.sismics.docs.core.util.jpa.SortCriteria; +import com.sismics.docs.rest.constant.BaseFunction; +import com.sismics.rest.exception.ForbiddenClientException; +import com.sismics.rest.util.ValidationUtil; + +import javax.json.Json; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObjectBuilder; +import javax.ws.rs.*; +import javax.ws.rs.core.Response; +import java.util.List; + +/** + * Metadata REST resources. + * + * @author bgamard + */ +@Path("/metadata") +public class MetadataResource extends BaseResource { + /** + * Returns the list of all configured metadata. + * + * @api {get} /metadata Get configured metadata + * @apiName GetMetadata + * @apiGroup Metadata + * @apiParam {Number} sort_column Column index to sort on + * @apiParam {Boolean} asc If true, sort in ascending order + * @apiSuccess {Object[]} metadata List of metadata + * @apiSuccess {String} metadata.id ID + * @apiSuccess {String} metadata.name Name + * @apiSuccess {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} metadata.type Type + * @apiError (client) ForbiddenError Access denied + * @apiPermission admin + * @apiVersion 1.7.0 + * + * @return Response + */ + @GET + public Response list( + @QueryParam("sort_column") Integer sortColumn, + @QueryParam("asc") Boolean asc) { + if (!authenticate()) { + throw new ForbiddenClientException(); + } + + JsonArrayBuilder metadata = Json.createArrayBuilder(); + SortCriteria sortCriteria = new SortCriteria(sortColumn, asc); + + MetadataDao metadataDao = new MetadataDao(); + List metadataDtoList = metadataDao.findByCriteria(new MetadataCriteria(), sortCriteria); + for (MetadataDto metadataDto : metadataDtoList) { + metadata.add(Json.createObjectBuilder() + .add("id", metadataDto.getId()) + .add("name", metadataDto.getName()) + .add("type", metadataDto.getType().name())); + } + + JsonObjectBuilder response = Json.createObjectBuilder() + .add("metadata", metadata); + return Response.ok().entity(response.build()).build(); + } + + /** + * Add a metadata. + * + * @api {put} /metadata Add a custom metadata + * @apiName PutMetadata + * @apiGroup Metadata + * @apiParam {String{1..50}} name Name + * @apiParam {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} type Type + * @apiSuccess {String} id ID + * @apiSuccess {String} name Name + * @apiSuccess {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} type Type + * @apiError (client) ForbiddenError Access denied + * @apiError (client) ValidationError Validation error + * @apiPermission admin + * @apiVersion 1.7.0 + * + * @param name Name + * @param typeStr Type + * @return Response + */ + @PUT + public Response add(@FormParam("name") String name, + @FormParam("type") String typeStr) { + if (!authenticate()) { + throw new ForbiddenClientException(); + } + checkBaseFunction(BaseFunction.ADMIN); + + // Validate input data + name = ValidationUtil.validateLength(name, "name", 1, 50, false); + MetadataType type = MetadataType.valueOf(ValidationUtil.validateLength(typeStr, "type", 1, 20, false)); + + // Create the metadata + MetadataDao metadataDao = new MetadataDao(); + Metadata metadata = new Metadata(); + metadata.setName(name); + metadata.setType(type); + metadataDao.create(metadata, principal.getId()); + + // Returns the metadata + JsonObjectBuilder response = Json.createObjectBuilder() + .add("id", metadata.getId()) + .add("name", metadata.getName()) + .add("type", metadata.getType().name()); + return Response.ok().entity(response.build()).build(); + } + + /** + * Update a metadata. + * + * @api {post} /metadata/:id Update a custom metadata + * @apiName PostMetadataId + * @apiGroup Metadata + * @apiParam {String} id Metadata ID + * @apiParam {String{1..50}} name Name + * @apiSuccess {String} id ID + * @apiSuccess {String} name Name + * @apiSuccess {String="STRING","INTEGER","FLOAT","DATE","BOOLEAN"} type Type + * @apiError (client) ForbiddenError Access denied + * @apiError (client) ValidationError Validation error + * @apiError (client) NotFound Metadata not found + * @apiPermission admin + * @apiVersion 1.7.0 + * + * @param id ID + * @param name Name + * @return Response + */ + @POST + @Path("{id: [a-z0-9\\-]+}") + public Response update(@PathParam("id") String id, + @FormParam("name") String name) { + if (!authenticate()) { + throw new ForbiddenClientException(); + } + checkBaseFunction(BaseFunction.ADMIN); + + // Validate input data + name = ValidationUtil.validateLength(name, "name", 1, 50, false); + + // Get the metadata + MetadataDao metadataDao = new MetadataDao(); + Metadata metadata = metadataDao.getActiveById(id); + if (metadata == null) { + throw new NotFoundException(); + } + + // Update the metadata + metadata.setName(name); + metadataDao.update(metadata, principal.getId()); + + // Returns the metadata + JsonObjectBuilder response = Json.createObjectBuilder() + .add("id", metadata.getId()) + .add("name", metadata.getName()) + .add("type", metadata.getType().name()); + return Response.ok().entity(response.build()).build(); + } + + /** + * Delete a metadata. + * + * @api {delete} /metadata/:id Delete a custom metadata + * @apiName DeleteMetadataId + * @apiGroup Metadata + * @apiParam {String} id Metadata ID + * @apiSuccess {String} status Status OK + * @apiError (client) ForbiddenError Access denied + * @apiError (client) NotFound Metadata not found + * @apiPermission admin + * @apiVersion 1.7.0 + * + * @param id ID + * @return Response + */ + @DELETE + @Path("{id: [a-z0-9\\-]+}") + public Response delete(@PathParam("id") String id) { + if (!authenticate()) { + throw new ForbiddenClientException(); + } + checkBaseFunction(BaseFunction.ADMIN); + + // Get the metadata + MetadataDao metadataDao = new MetadataDao(); + Metadata metadata = metadataDao.getActiveById(id); + if (metadata == null) { + throw new NotFoundException(); + } + + // Delete the metadata + metadataDao.delete(id, principal.getId()); + + // Always return OK + JsonObjectBuilder response = Json.createObjectBuilder() + .add("status", "ok"); + return Response.ok().entity(response.build()).build(); + } +} diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/VocabularyResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/VocabularyResource.java index 7063c7c8..2f02909d 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/VocabularyResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/VocabularyResource.java @@ -198,7 +198,7 @@ public class VocabularyResource extends BaseResource { /** * Delete a vocabulary entry. * - * @api {delete} /vocabulary/:id Delete vocabulary entry + * @api {delete} /vocabulary/:id Delete a vocabulary entry * @apiName DeleteVocabularyId * @apiGroup Vocabulary * @apiParam {String} id Entry ID diff --git a/docs-web/src/prod/resources/config.properties b/docs-web/src/prod/resources/config.properties index 8e983362..9dd002b9 100644 --- a/docs-web/src/prod/resources/config.properties +++ b/docs-web/src/prod/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=23 \ No newline at end of file +db.version=24 \ No newline at end of file diff --git a/docs-web/src/stress/resources/config.properties b/docs-web/src/stress/resources/config.properties index 8e983362..9dd002b9 100644 --- a/docs-web/src/stress/resources/config.properties +++ b/docs-web/src/stress/resources/config.properties @@ -1,3 +1,3 @@ api.current_version=${project.version} api.min_version=1.0 -db.version=23 \ No newline at end of file +db.version=24 \ No newline at end of file diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestMetadataResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestMetadataResource.java new file mode 100644 index 00000000..630c684a --- /dev/null +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestMetadataResource.java @@ -0,0 +1,82 @@ +package com.sismics.docs.rest; + +import com.sismics.util.filter.TokenBasedSecurityFilter; +import org.junit.Assert; +import org.junit.Test; + +import javax.json.JsonArray; +import javax.json.JsonObject; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Form; + + +/** + * Test the metadata resource. + * + * @author bgamard + */ +public class TestMetadataResource extends BaseJerseyTest { + /** + * Test the metadata resource. + */ + @Test + public void testMetadataResource() { + // Login admin + String adminToken = clientUtil.login("admin", "admin", false); + + // Get all metadata with admin + JsonObject json = target().path("/metadata") + .queryParam("sort_column", "2") + .queryParam("asc", "false") + .request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .get(JsonObject.class); + JsonArray metadata = json.getJsonArray("metadata"); + Assert.assertEquals(0, metadata.size()); + + // Create a metadata with admin + json = target().path("/metadata").request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .put(Entity.form(new Form() + .param("name", "ISBN 13") + .param("type", "STRING")), JsonObject.class); + String metadataIsbnId = json.getString("id"); + Assert.assertNotNull(metadataIsbnId); + Assert.assertEquals("ISBN 13", json.getString("name")); + Assert.assertEquals("STRING", json.getString("type")); + + // Get all metadata with admin + json = target().path("/metadata") + .queryParam("sort_column", "2") + .queryParam("asc", "false") + .request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .get(JsonObject.class); + metadata = json.getJsonArray("metadata"); + Assert.assertEquals(1, metadata.size()); + + // Update a metadata with admin + json = target().path("/metadata/" + metadataIsbnId).request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .post(Entity.form(new Form() + .param("name", "ISBN 10")), JsonObject.class); + Assert.assertEquals(metadataIsbnId, json.getString("id")); + Assert.assertEquals("ISBN 10", json.getString("name")); + Assert.assertEquals("STRING", json.getString("type")); + + // Delete a metadata with admin + target().path("/metadata/" + metadataIsbnId).request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .delete(JsonObject.class); + + // Get all metadata with admin + json = target().path("/metadata") + .queryParam("sort_column", "2") + .queryParam("asc", "false") + .request() + .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) + .get(JsonObject.class); + metadata = json.getJsonArray("metadata"); + Assert.assertEquals(0, metadata.size()); + } +} \ No newline at end of file diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestVocabularyResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestVocabularyResource.java index 9fd83517..869c39b0 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestVocabularyResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestVocabularyResource.java @@ -1,16 +1,15 @@ package com.sismics.docs.rest; +import com.sismics.util.filter.TokenBasedSecurityFilter; +import org.junit.Assert; +import org.junit.Test; + 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; - -import com.sismics.util.filter.TokenBasedSecurityFilter; - /** * Exhaustive test of the vocabulary resource. * @@ -100,7 +99,7 @@ public class TestVocabularyResource extends BaseJerseyTest { Assert.assertEquals(1, entry.getJsonNumber("order").intValue()); // Delete a vocabulary entry with admin - json = target().path("/vocabulary/" + vocabulary1Id).request() + target().path("/vocabulary/" + vocabulary1Id).request() .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) .delete(JsonObject.class);