From 4b6c367857e7afa4270281bf48652f0160dc079a Mon Sep 17 00:00:00 2001 From: Jing Ma Date: Wed, 31 Mar 2021 14:41:59 +0200 Subject: [PATCH] DD-387 Add license entity and api (#57) * First db table and api. * Final changes for prototype. * Add integration tests. * Fix indentation. * Add prototype of newest changes. * Add URI and URL objects, and new endpoints. * Add Apache icons. * Change tokens to licenses. * Change URIException to IllegalStateException. --- scripts/api/data/license.json | 7 + scripts/api/data/licenseError.json | 8 + scripts/api/data/licenseUpdate.json | 7 + .../edu/harvard/iq/dataverse/License.java | 154 ++++++++++++++++++ .../iq/dataverse/LicenseServiceBean.java | 119 ++++++++++++++ .../edu/harvard/iq/dataverse/api/Admin.java | 98 ++++++++++- .../iq/dataverse/api/FetchException.java | 17 ++ .../dataverse/api/RequestBodyException.java | 17 ++ .../iq/dataverse/api/UpdateException.java | 17 ++ .../iq/dataverse/util/json/JsonPrinter.java | 12 ++ .../edu/harvard/iq/dataverse/api/AdminIT.java | 81 +++++++++ .../edu/harvard/iq/dataverse/api/UtilIT.java | 65 ++++++++ 12 files changed, 597 insertions(+), 5 deletions(-) create mode 100644 scripts/api/data/license.json create mode 100644 scripts/api/data/licenseError.json create mode 100644 scripts/api/data/licenseUpdate.json create mode 100644 src/main/java/edu/harvard/iq/dataverse/License.java create mode 100644 src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java create mode 100644 src/main/java/edu/harvard/iq/dataverse/api/FetchException.java create mode 100644 src/main/java/edu/harvard/iq/dataverse/api/RequestBodyException.java create mode 100644 src/main/java/edu/harvard/iq/dataverse/api/UpdateException.java diff --git a/scripts/api/data/license.json b/scripts/api/data/license.json new file mode 100644 index 00000000000..11e0d44c14b --- /dev/null +++ b/scripts/api/data/license.json @@ -0,0 +1,7 @@ +{ + "name": "Apache License 1.0", + "shortDescription": "This is the original Apache License which applies only to very old versions of Apache packages (such as version 1.2 of the Web server).", + "uri": "https://www.apache.org/licenses/LICENSE-1.0", + "iconUrl": "https://itgala.xyz/wp-content/uploads/2017/10/Apache-HTTP-Server.png", + "active": false +} \ No newline at end of file diff --git a/scripts/api/data/licenseError.json b/scripts/api/data/licenseError.json new file mode 100644 index 00000000000..d6b1dbbd01b --- /dev/null +++ b/scripts/api/data/licenseError.json @@ -0,0 +1,8 @@ +{ + "id": 6, + "name": "Apache License 1.0", + "shortDescription": "This is the original Apache License which applies only to very old versions of Apache packages (such as version 1.2 of the Web server).", + "uri": "https://www.apache.org/licenses/LICENSE-1.0", + "iconUrl": "https://itgala.xyz/wp-content/uploads/2017/10/Apache-HTTP-Server.png", + "active": false +} \ No newline at end of file diff --git a/scripts/api/data/licenseUpdate.json b/scripts/api/data/licenseUpdate.json new file mode 100644 index 00000000000..eefc4e6f16f --- /dev/null +++ b/scripts/api/data/licenseUpdate.json @@ -0,0 +1,7 @@ +{ + "name": "Apache License 2.0", + "shortDescription": "The 2.0 version of the Apache License, approved by the ASF in 2004.", + "uri": "https://www.apache.org/licenses/LICENSE-2.0", + "iconUrl": "https://itgala.xyz/wp-content/uploads/2017/10/Apache-HTTP-Server.png", + "active": true +} \ No newline at end of file diff --git a/src/main/java/edu/harvard/iq/dataverse/License.java b/src/main/java/edu/harvard/iq/dataverse/License.java new file mode 100644 index 00000000000..29653271e01 --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/License.java @@ -0,0 +1,154 @@ +package edu.harvard.iq.dataverse; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Objects; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; + +/** + * @author Jing Ma + */ + @NamedQueries({ + @NamedQuery( name="License.findAll", + query="SELECT l FROM License l"), + @NamedQuery( name="License.findById", + query = "SELECT l FROM License l WHERE l.id=:id"), + @NamedQuery( name="License.findByName", + query = "SELECT l FROM License l WHERE l.name=:name"), + @NamedQuery( name="License.deleteById", + query="DELETE FROM License l WHERE l.id=:id"), + @NamedQuery( name="License.deleteByName", + query="DELETE FROM License l WHERE l.name=:name") +}) +@Entity +@Table(uniqueConstraints = { + @UniqueConstraint(columnNames = "name"), + @UniqueConstraint(columnNames = "uri")} +) +public class License { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(columnDefinition="TEXT", nullable = false) + private String name; + + @Column(columnDefinition="TEXT") + private String shortDescription; + + @Column(columnDefinition="TEXT", nullable = false) + private String uri; + + @Column(columnDefinition="TEXT") + private String iconUrl; + + @Column(nullable = false) + private boolean active; + + public License() { + } + + public License(String name, String shortDescription, URI uri, URI iconUrl, boolean active) { + this.name = name; + this.shortDescription = shortDescription; + this.uri = uri.toASCIIString(); + this.iconUrl = iconUrl.toASCIIString(); + this.active = active; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getShortDescription() { + return shortDescription; + } + + public void setShortDescription(String shortDescription) { + this.shortDescription = shortDescription; + } + + public URI getUri() { + try { + return new URI(uri); + } catch (URISyntaxException e) { + throw new IllegalStateException("Incorrect URI in JSON"); + } + } + + public void setUri(URI uri) { + this.uri = uri.toASCIIString(); + } + + public URI getIconUrl() { + try { + return new URI(iconUrl); + } catch (URISyntaxException e) { + throw new IllegalStateException("Incorrect URI in JSON"); + } + } + + public void setIconUrl(URI iconUrl) { + this.iconUrl = iconUrl.toASCIIString(); + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + License license = (License) o; + return active == license.active && + Objects.equals(id, license.id) && + Objects.equals(name, license.name) && + Objects.equals(shortDescription, license.shortDescription) && + Objects.equals(uri, license.uri) && + Objects.equals(iconUrl, license.iconUrl); + } + + @Override + public int hashCode() { + return Objects.hash(id, name, shortDescription, uri, iconUrl, active); + } + + @Override + public String toString() { + return "License{" + + "id=" + id + + ", name='" + name + '\'' + + ", shortDescription='" + shortDescription + '\'' + + ", uri=" + uri + + ", iconUrl=" + iconUrl + + ", active=" + active + + '}'; + } + +} diff --git a/src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java b/src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java new file mode 100644 index 00000000000..c49ebd9659e --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java @@ -0,0 +1,119 @@ +package edu.harvard.iq.dataverse; + +import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord; +import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean; +import edu.harvard.iq.dataverse.api.FetchException; +import edu.harvard.iq.dataverse.api.RequestBodyException; +import edu.harvard.iq.dataverse.api.UpdateException; +import java.net.URI; +import java.net.URL; +import java.util.List; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import javax.inject.Named; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceException; + +/** + * @author Jing Ma + */ +@Stateless +@Named +public class LicenseServiceBean { + + @PersistenceContext + EntityManager em; + + @EJB + ActionLogServiceBean actionLogSvc; + + public List listAll() { + return em.createNamedQuery("License.findAll", License.class).getResultList(); + } + + public License getById(long id) throws FetchException { + List licenses = em.createNamedQuery("License.findById", License.class) + .setParameter("id", id ) + .getResultList(); + if (licenses.isEmpty()) { + throw new FetchException("License with that ID doesn't exist."); + } + return licenses.get(0); + } + + public License getByName(String name) throws FetchException { + List licenses = em.createNamedQuery("License.findByName", License.class) + .setParameter("name", name ) + .getResultList(); + if (licenses.isEmpty()) { + throw new FetchException("License with that name doesn't exist."); + } + return licenses.get(0); + } + + public License save(License license) throws PersistenceException, RequestBodyException { + if (license.getId() == null) { + em.persist(license); + return license; + } else { + throw new RequestBodyException("There shouldn't be an ID in the request body"); + } + } + + public void setById(long id, String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException { + List licenses = em.createNamedQuery("License.findById", License.class) + .setParameter("id", id ) + .getResultList(); + + if(licenses.size() > 0) { + License license = licenses.get(0); + license.setName(name); + license.setShortDescription(shortDescription); + license.setUri(uri); + license.setIconUrl(iconUrl); + license.setActive(active); + em.merge(license); + actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "set") + .setInfo(name + ": " + shortDescription + ": " + uri + ": " + iconUrl + ": " + active)); + } else { + throw new UpdateException("There is no existing License with that ID. To add a license use POST."); + } + } + + public void setByName(String name, String shortDescription, URI uri, URI iconUrl, boolean active) throws UpdateException { + List licenses = em.createNamedQuery("License.findByName", License.class) + .setParameter("name", name ) + .getResultList(); + + if(licenses.size() > 0) { + License license = licenses.get(0); + license.setShortDescription(shortDescription); + license.setUri(uri); + license.setIconUrl(iconUrl); + license.setActive(active); + em.merge(license); + actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "set") + .setInfo(name + ": " + shortDescription + ": " + uri + ": " + iconUrl + ": " + active)); + } else { + throw new UpdateException("There is no existing License with that name. To add a license use POST."); + } + } + + public int deleteById(long id) throws PersistenceException { + actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "delete") + .setInfo(Long.toString(id))); + return em.createNamedQuery("License.deleteById") + .setParameter("id", id) + .executeUpdate(); + } + + public int deleteByName(String name) throws PersistenceException { + actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "delete") + .setInfo(name)); + return em.createNamedQuery("License.deleteByName") + .setParameter("name", name) + .executeUpdate(); + } + +} diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Admin.java b/src/main/java/edu/harvard/iq/dataverse/api/Admin.java index b52665a7747..fc7cf73d505 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Admin.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Admin.java @@ -16,7 +16,8 @@ import edu.harvard.iq.dataverse.EMailValidator; import edu.harvard.iq.dataverse.EjbDataverseEngine; import edu.harvard.iq.dataverse.GlobalId; -import edu.harvard.iq.dataverse.RoleAssignment; +import edu.harvard.iq.dataverse.License; +import edu.harvard.iq.dataverse.LicenseServiceBean; import edu.harvard.iq.dataverse.UserServiceBean; import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord; import edu.harvard.iq.dataverse.api.dto.RoleDTO; @@ -42,9 +43,11 @@ import edu.harvard.iq.dataverse.engine.command.impl.AbstractSubmitToArchiveCommand; import edu.harvard.iq.dataverse.engine.command.impl.PublishDataverseCommand; import edu.harvard.iq.dataverse.settings.Setting; +import edu.harvard.iq.dataverse.util.json.JsonPrinter; import javax.json.Json; import javax.json.JsonArrayBuilder; import javax.json.JsonObjectBuilder; +import javax.persistence.PersistenceException; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -71,7 +74,6 @@ import javax.ws.rs.core.Response.Status; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.StringUtils; import java.util.List; import edu.harvard.iq.dataverse.authorization.AuthTestDataServiceBean; @@ -85,8 +87,6 @@ import edu.harvard.iq.dataverse.dataset.DatasetUtil; import edu.harvard.iq.dataverse.engine.command.DataverseRequest; import edu.harvard.iq.dataverse.engine.command.exception.CommandException; -import edu.harvard.iq.dataverse.engine.command.impl.MergeInAccountCommand; -import edu.harvard.iq.dataverse.engine.command.impl.ChangeUserIdentifierCommand; import edu.harvard.iq.dataverse.engine.command.impl.RegisterDvObjectCommand; import edu.harvard.iq.dataverse.ingest.IngestServiceBean; import edu.harvard.iq.dataverse.settings.SettingsServiceBean; @@ -101,7 +101,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Date; -import java.util.function.Consumer; import javax.inject.Inject; import javax.json.JsonArray; import javax.persistence.Query; @@ -152,6 +151,8 @@ public class Admin extends AbstractApiBean { ExplicitGroupServiceBean explicitGroupService; @EJB BannerMessageServiceBean bannerMessageService; + @EJB + LicenseServiceBean licenseService; // Make the session available @@ -1920,4 +1921,91 @@ public Response getBannerMessages(@PathParam("id") Long id) throws WrappedRespon } + @GET + @Path("/licenses") + public Response getLicenses() { + JsonArrayBuilder arrayBuilder = Json.createArrayBuilder(); + for(License license : licenseService.listAll()) { + arrayBuilder.add(JsonPrinter.json(license)); + } + return ok(arrayBuilder); + } + + @GET + @Path("/licenses/id/{id}") + public Response getLicenseById(@PathParam("id") long id) { + try { + License license = licenseService.getById(id); + return ok(json(license)); + } catch (FetchException e) { + return error(Response.Status.NOT_FOUND, e.getMessage()); + } + } + + @GET + @Path("/licenses/name/{name}") + public Response getLicenseByName(@PathParam("name") String name) { + try { + License license = licenseService.getByName(name); + return ok(json(license)); + } catch (FetchException e) { + return error(Response.Status.NOT_FOUND, e.getMessage()); + } + } + + @POST + @Path("/licenses") + public Response addLicense(License license) { + try { + licenseService.save(license); + return created("/api/admin/licenses", Json.createObjectBuilder().add("message", "License created")); + } catch (RequestBodyException e) { + return error(Response.Status.BAD_REQUEST, e.getMessage()); + } catch(PersistenceException e) { + return error(Response.Status.CONFLICT, "A license with the same URI or name is already present."); + } + } + + @PUT + @Path("/licenses/id/{id}") + public Response putLicenseById(@PathParam("id") long id, License license) { + try { + licenseService.setById(id, license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive()); + } catch (UpdateException e) { + return error(Response.Status.BAD_REQUEST, e.getMessage()); + } + return ok("License with ID " + id + " was replaced."); + } + + @PUT + @Path("/licenses/name/{name}") + public Response putLicenseByName(@PathParam("name") String name, License license) { + try { + licenseService.setByName(license.getName(), license.getShortDescription(), license.getUri(), license.getIconUrl(), license.isActive()); + } catch (UpdateException e) { + return error(Response.Status.BAD_REQUEST, e.getMessage()); + } + return ok("License with name " + name + " was replaced."); + } + + @DELETE + @Path("/licenses/id/{id}") + public Response deleteLicenseById(@PathParam("id") long id) { + int result = licenseService.deleteById(id); + if (result == 1) { + return ok("OK. License with ID " + id + " was deleted."); + } + return error(Response.Status.NOT_FOUND, "A license with ID " + id + " doesn't exist."); + } + + @DELETE + @Path("/licenses/name/{name}") + public Response deleteLicenseByName(@PathParam("name") String name) { + int result = licenseService.deleteByName(name); + if (result == 1) { + return ok("OK. License with name " + name + " was deleted."); + } + return error(Response.Status.NOT_FOUND, "A license with name " + name + " doesn't exist."); + } + } diff --git a/src/main/java/edu/harvard/iq/dataverse/api/FetchException.java b/src/main/java/edu/harvard/iq/dataverse/api/FetchException.java new file mode 100644 index 00000000000..a9c77c7a4c5 --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/api/FetchException.java @@ -0,0 +1,17 @@ +package edu.harvard.iq.dataverse.api; + +/** + * + * @author Jing Ma + */ +public class FetchException extends Exception { + + public FetchException(String message) { + super(message); + } + + public FetchException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/edu/harvard/iq/dataverse/api/RequestBodyException.java b/src/main/java/edu/harvard/iq/dataverse/api/RequestBodyException.java new file mode 100644 index 00000000000..e78c87abdfa --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/api/RequestBodyException.java @@ -0,0 +1,17 @@ +package edu.harvard.iq.dataverse.api; + +/** + * + * @author Jing Ma + */ +public class RequestBodyException extends Exception { + + public RequestBodyException(String message) { + super(message); + } + + public RequestBodyException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/edu/harvard/iq/dataverse/api/UpdateException.java b/src/main/java/edu/harvard/iq/dataverse/api/UpdateException.java new file mode 100644 index 00000000000..4dbd3ab19a3 --- /dev/null +++ b/src/main/java/edu/harvard/iq/dataverse/api/UpdateException.java @@ -0,0 +1,17 @@ +package edu.harvard.iq.dataverse.api; + +/** + * + * @author Jing Ma + */ +public class UpdateException extends Exception { + + public UpdateException(String message) { + super(message); + } + + public UpdateException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java b/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java index c37efc3178f..7a5334114e7 100644 --- a/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java +++ b/src/main/java/edu/harvard/iq/dataverse/util/json/JsonPrinter.java @@ -16,6 +16,7 @@ import edu.harvard.iq.dataverse.DataverseContact; import edu.harvard.iq.dataverse.DataverseFacet; import edu.harvard.iq.dataverse.DataverseTheme; +import edu.harvard.iq.dataverse.License; import edu.harvard.iq.dataverse.authorization.DataverseRole; import edu.harvard.iq.dataverse.authorization.groups.impl.maildomain.MailDomainGroup; import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser; @@ -44,6 +45,7 @@ import edu.harvard.iq.dataverse.workflow.Workflow; import edu.harvard.iq.dataverse.workflow.step.WorkflowStepData; +import java.net.URISyntaxException; import java.util.*; import javax.json.Json; import javax.json.JsonArrayBuilder; @@ -775,6 +777,16 @@ public static JsonObjectBuilder json( DataverseFacet aFacet ) { .add("id", String.valueOf(aFacet.getId())) // TODO should just be id I think .add("name", aFacet.getDatasetFieldType().getDisplayName()); } + + public static JsonObjectBuilder json(License license) { + return jsonObjectBuilder() + .add("id", license.getId()) + .add("name", license.getName()) + .add("shortDescription", license.getShortDescription()) + .add("uri", license.getUri().toString()) + .add("iconUrl", license.getIconUrl().toString()) + .add("active", license.isActive()); + } public static Collector stringsToJsonArray() { return new Collector() { diff --git a/src/test/java/edu/harvard/iq/dataverse/api/AdminIT.java b/src/test/java/edu/harvard/iq/dataverse/api/AdminIT.java index f83e9d9c839..a1bcc0b08fd 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/AdminIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/AdminIT.java @@ -786,4 +786,85 @@ public void testBannerMessages(){ assertEquals("OK", status); } + + @Test + public void testLicenses(){ + + String pathToJsonFile = "scripts/api/data/license.json"; + Response addLicenseResponse = UtilIT.addLicense(pathToJsonFile); + addLicenseResponse.prettyPrint(); + String body = addLicenseResponse.getBody().asString(); + String status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + pathToJsonFile = "scripts/api/data/licenseError.json"; + Response addLicenseErrorResponse = UtilIT.addLicense(pathToJsonFile); + addLicenseErrorResponse.prettyPrint(); + body = addLicenseErrorResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("ERROR", status); + + Response getLicensesResponse = UtilIT.getLicenses(); + getLicensesResponse.prettyPrint(); + body = getLicensesResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + Response getLicenseByIdResponse = UtilIT.getLicenseById(1L); + getLicenseByIdResponse.prettyPrint(); + body = getLicenseByIdResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + Response getLicenseByNameResponse = UtilIT.getLicenseByName(""); + getLicenseByNameResponse.prettyPrint(); + body = getLicenseByNameResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + Response getLicenseErrorResponse = UtilIT.getLicenseById(10L); + getLicenseErrorResponse.prettyPrint(); + body = getLicenseErrorResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("ERROR", status); + + pathToJsonFile = "scripts/api/data/licenseUpdate.json"; + Response updateLicenseByIdResponse = UtilIT.updateLicenseById(pathToJsonFile, 1L); + updateLicenseByIdResponse.prettyPrint(); + body = updateLicenseByIdResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + pathToJsonFile = "scripts/api/data/licenseUpdate.json"; + Response updateLicenseByNameResponse = UtilIT.updateLicenseByName(pathToJsonFile, ""); + updateLicenseByNameResponse.prettyPrint(); + body = updateLicenseByNameResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + Response updateLicenseErrorResponse = UtilIT.updateLicenseById(pathToJsonFile, 10L); + updateLicenseErrorResponse.prettyPrint(); + body = updateLicenseErrorResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("ERROR", status); + + Response deleteLicenseByIdResponse = UtilIT.deleteLicenseById(1L); + deleteLicenseByIdResponse.prettyPrint(); + body = deleteLicenseByIdResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + Response deleteLicenseByNameResponse = UtilIT.deleteLicenseByName(""); + deleteLicenseByNameResponse.prettyPrint(); + body = deleteLicenseByNameResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("OK", status); + + Response deleteLicenseErrorResponse = UtilIT.deleteLicenseById(10L); + deleteLicenseErrorResponse.prettyPrint(); + body = deleteLicenseErrorResponse.getBody().asString(); + status = JsonPath.from(body).getString("status"); + assertEquals("ERROR", status); + + } } diff --git a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java index f3ff8f8fae4..c5f4da033d1 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -2523,5 +2523,70 @@ static String getBannerMessageIdFromResponse(String getBannerMessagesResponse) { return "0"; } + static Response addLicense(String pathToJsonFile) { + String jsonIn = getDatasetJson(pathToJsonFile); + + Response addLicenseResponse = given() + .body(jsonIn) + .contentType("application/json") + .post("/api/admin/licenses"); + return addLicenseResponse; + } + + static Response getLicenses() { + + Response getLicensesResponse = given() + .get("/api/admin/licenses"); + return getLicensesResponse; + } + + static Response getLicenseById(Long id) { + + Response getLicenseResponse = given() + .get("/api/admin/licenses/id/"+id.toString()); + return getLicenseResponse; + } + + static Response getLicenseByName(String name) { + + Response getLicenseResponse = given() + .get("/api/admin/licenses/name/"+name); + return getLicenseResponse; + } + + static Response updateLicenseById(String pathToJsonFile, Long id) { + String jsonIn = getDatasetJson(pathToJsonFile); + + Response updateLicenseResponse = given() + .body(jsonIn) + .contentType("application/json") + .put("/api/admin/licenses/id/"+id.toString()); + return updateLicenseResponse; + } + + static Response updateLicenseByName(String pathToJsonFile, String name) { + String jsonIn = getDatasetJson(pathToJsonFile); + + Response updateLicenseResponse = given() + .body(jsonIn) + .contentType("application/json") + .put("/api/admin/licenses/name/"+name); + return updateLicenseResponse; + } + + static Response deleteLicenseById(Long id) { + + Response deleteLicenseResponse = given() + .delete("/api/admin/licenses/id/"+id.toString()); + return deleteLicenseResponse; + } + + static Response deleteLicenseByName(String name) { + + Response deleteLicenseResponse = given() + .delete("/api/admin/licenses/name/"+name); + return deleteLicenseResponse; + } + }