Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error response/AdminITlicense tests #137

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/main/java/edu/harvard/iq/dataverse/LicenseServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.harvard.iq.dataverse.actionlogging.ActionLogRecord;
import edu.harvard.iq.dataverse.actionlogging.ActionLogServiceBean;
import edu.harvard.iq.dataverse.api.AbstractApiBean.WrappedResponse;

import javax.ejb.EJB;
import javax.ejb.Stateless;
Expand Down Expand Up @@ -64,28 +65,28 @@ public License getByNameOrUri(String nameOrUri) {
}
}

public int setDefault(Long id){
public int setDefault(Long id) throws WrappedResponse{
License candidate = getById(id);
if (candidate == null) return 0;
if (candidate.isActive()) {
em.createNamedQuery("License.clearDefault").executeUpdate();
return em.createNamedQuery("License.setDefault").setParameter("id", id).executeUpdate();
} else {
throw new IllegalArgumentException("Cannot set an inactive license as default");
throw new WrappedResponse(new IllegalArgumentException("Cannot set an inactive license as default"), null);
}
}

public License save(License license) {
public License save(License license) throws WrappedResponse {
if (license.getId() != null) {
throw new IllegalArgumentException("There shouldn't be an ID in the request body");
throw new WrappedResponse(new IllegalArgumentException("There shouldn't be an ID in the request body"), null);
}
try {
em.persist(license);
em.flush();
}
catch (PersistenceException p) {
if (p.getMessage().contains("duplicate key")) {
throw new IllegalStateException("A license with the same URI or name is already present.", p);
throw new WrappedResponse(new IllegalStateException("A license with the same URI or name is already present.", p), null);
}
else {
throw p;
Expand All @@ -94,14 +95,14 @@ public License save(License license) {
return license;
}

public int deleteById(long id) {
public int deleteById(long id) throws WrappedResponse {
actionLogSvc.log( new ActionLogRecord(ActionLogRecord.ActionType.Admin, "delete")
.setInfo(Long.toString(id)));
try {
return em.createNamedQuery("License.deleteById").setParameter("id", id).executeUpdate();
} catch (PersistenceException p) {
if (p.getMessage().contains("violates foreign key constraint")) {
throw new IllegalStateException("License with id " + id + " is referenced and cannot be deleted.", p);
throw new WrappedResponse(new IllegalStateException("License with id " + id + " is referenced and cannot be deleted.", p), null);
} else {
throw p;
}
Expand Down
75 changes: 43 additions & 32 deletions src/main/java/edu/harvard/iq/dataverse/api/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2079,9 +2079,10 @@ public Response getLicenses() {
@GET
@Path("/licenses/{id}")
public Response getLicenseById(@PathParam("id") long id) {
License license = licenseService.getById(id);
if (license == null) return error(Response.Status.NOT_FOUND, "License with ID " + id + " not found");
return ok(json(license));
License license = licenseService.getById(id);
if (license == null)
return error(Response.Status.NOT_FOUND, "License with ID " + id + " not found");
return ok(json(license));
}

@POST
Expand All @@ -2090,49 +2091,59 @@ public Response addLicense(License license) {
try {
licenseService.save(license);
return created("/api/admin/licenses", Json.createObjectBuilder().add("message", "License created"));
} catch (IllegalArgumentException e) {
return error(Response.Status.BAD_REQUEST, e.getMessage());
} catch(IllegalStateException e) {
return error(Response.Status.CONFLICT, e.getMessage());
} catch (WrappedResponse e) {
Throwable cause = e.getCause();
if (cause instanceof IllegalArgumentException) {
return error(Response.Status.BAD_REQUEST, cause.getMessage());
} else if (cause instanceof IllegalStateException) {
return error(Response.Status.CONFLICT, cause.getMessage());
}
return error(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());
}
}

@GET
@Path("/licenses/default")
public Response getDefault(){
return ok("Default license ID is " + licenseService.getDefault().getId());
}
@GET
@Path("/licenses/default")
public Response getDefault() {
return ok("Default license ID is " + licenseService.getDefault().getId());
}

@PUT
@Path("/licenses/default/{id}")
public Response setDefault(@PathParam("id") long id) {
try {
if (licenseService.setDefault(id) == 0) return error(Response.Status.NOT_FOUND, "License with ID " + id + " not found");
if (licenseService.setDefault(id) == 0)
return error(Response.Status.NOT_FOUND, "License with ID " + id + " not found");
return ok("Default license ID set to " + id);
}
catch (IllegalArgumentException illegalArgumentException) {
return badRequest(illegalArgumentException.getMessage());
} catch (WrappedResponse e) {
if(e.getCause() instanceof IllegalArgumentException) {
return badRequest(e.getCause().getMessage());
}
return error(Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());
}
}

@DELETE
@Path("/licenses/{id}")
public Response deleteLicenseById(@PathParam("id") long id) {
try {
License license = licenseService.getById(id);
if (license == null) {
return error(Status.NOT_FOUND, "License with ID " + id + " not found");
} else if (license.isDefault()){
return error(Status.CONFLICT, "Please make sure the license is not the default before deleting it. ");
} else {
if (licenseService.deleteById(id) == 1) {
return ok("OK. License with ID " + id + " was deleted.");
} else {
return error(Status.CONFLICT, "Couldn't delete license with ID: " + id);
}
}
} catch(IllegalStateException e) {
return error(Response.Status.CONFLICT, e.getMessage());
}
try {
License license = licenseService.getById(id);
if (license == null) {
return error(Status.NOT_FOUND, "License with ID " + id + " not found");
} else if (license.isDefault()) {
return error(Status.CONFLICT, "Please make sure the license is not the default before deleting it. ");
} else {
if (licenseService.deleteById(id) == 1) {
return ok("OK. License with ID " + id + " was deleted.");
} else {
return error(Status.CONFLICT, "Couldn't delete license with ID: " + id);
}
}
} catch (WrappedResponse e) {
if (e.getCause() instanceof IllegalStateException) {
return error(Response.Status.CONFLICT, e.getMessage());
}
return error(Status.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
}