PrivilegeApiResource extends PrivilegeApiResourceSupport implements Resource, PrivilegeApiResourceDoc { @Override @DELETE @RequiresAuthentication @RequiresPermissions("nexus:privileges:delete") @Path("{privilegeId}") public void deletePrivilege(@PathParam("privilegeId") final String privilegeId) { try { getDefaultAuthorizationManager().deletePrivilege(privilegeId); } catch (NoSuchPrivilegeException e) { log.debug("Attempt to delete privilege '{}' failed, as it wasn't found in the system.", privilegeId, e); throw new WebApplicationMessageException(Status.NOT_FOUND, String.format(PRIV_NOT_FOUND, privilegeId), MediaType.APPLICATION_JSON); } catch (ReadonlyPrivilegeException e) { log.debug("Attempt to delete privilege '{}' failed, as it is readonly.", privilegeId, e); throw new WebApplicationMessageException(Status.BAD_REQUEST, String.format(PRIV_INTERNAL, privilegeId), MediaType.APPLICATION_JSON); } } @Inject  PrivilegeApiResource(final SecuritySystem securitySystem,
                              final Map<String, PrivilegeDescriptor> privilegeDescriptors); @Override @GET @RequiresAuthentication @RequiresPermissions("nexus:privileges:read") List<ApiPrivilege> getPrivileges(); @Override @GET @RequiresAuthentication @RequiresPermissions("nexus:privileges:read") @Path("{privilegeId}") ApiPrivilege getPrivilege(@PathParam("privilegeId") final String privilegeId); @Override @DELETE @RequiresAuthentication @RequiresPermissions("nexus:privileges:delete") @Path("{privilegeId}") void deletePrivilege(@PathParam("privilegeId") final String privilegeId); @Override @POST @RequiresAuthentication @RequiresPermissions("nexus:privileges:create") @Path("application") Response createPrivilege(final ApiPrivilegeApplicationRequest privilege); @Override @PUT @RequiresAuthentication @RequiresPermissions("nexus:privileges:update") @Path("application/{privilegeId}") void updatePrivilege(@PathParam("privilegeId") final String privilegeId,
                              final ApiPrivilegeApplicationRequest privilege); @Override @POST @RequiresAuthentication @RequiresPermissions("nexus:privileges:create") @Path("wildcard") Response createPrivilege(final ApiPrivilegeWildcardRequest privilege); @Override @PUT @RequiresAuthentication @RequiresPermissions("nexus:privileges:update") @Path("wildcard/{privilegeId}") void updatePrivilege(@PathParam("privilegeId") final String privilegeId,
                              final ApiPrivilegeWildcardRequest privilege);  }
@Test public void testDeletePrivilege() { Privilege priv = createPrivilege("wildcard", "priv", "privdesc", false, PATTERN_KEY, "a:pattern"); when(authorizationManager.getPrivilege("priv")).thenReturn(priv); underTest.deletePrivilege("priv"); verify(authorizationManager).deletePrivilege("priv"); }
@Test public void testDeletePrivilege_readOnly() { doThrow(new ReadonlyPrivilegeException("priv")).when(authorizationManager).deletePrivilege("priv"); try { underTest.deletePrivilege("priv"); fail("exception should have been thrown for internal privilege"); } catch (WebApplicationMessageException e) { assertThat(e.getResponse().getStatus(), is(400)); assertThat(e.getResponse().getMediaType(), is(MediaType.APPLICATION_JSON_TYPE)); assertThat(e.getResponse().getEntity().toString(), is("\"Privilege 'priv' is internal and cannot be modified or deleted.\"")); } }
@Test public void testDeletePrivilege_notFound() { doThrow(new NoSuchPrivilegeException("priv")).when(authorizationManager).deletePrivilege("priv"); try { underTest.deletePrivilege("priv"); fail("exception should have been thrown for missing privilege"); } catch (WebApplicationMessageException e) { assertThat(e.getResponse().getStatus(), is(404)); assertThat(e.getResponse().getMediaType(), is(MediaType.APPLICATION_JSON_TYPE)); assertThat(e.getResponse().getEntity().toString(), is("\"Privilege 'priv' not found.\"")); } }