OrganizationResource extends AbstractResource { @DELETE @Path("/{name}") public Response delete(@Auth final DbCredential credential, @PathParam("name") final String name){ if(!credential.getRoles().contains(DbCredential.AvailableRoles.DATA_DELETER)){ throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED).build()); } LOG.info("Got a delete organization request."); getOrganizationHandler().deleteOrganization(name); return Response.ok("done").build(); } OrganizationResource(final RepositoryHandler repositoryHandler, final GrapesServerConfig configuration); @POST Response postOrganization(@Auth final DbCredential credential, final Organization organization); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path(ServerAPI.GET_NAMES) Response getNames(); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}") Response get(@PathParam("name") final String name); @DELETE @Path("/{name}") Response delete(@Auth final DbCredential credential, @PathParam("name") final String name); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}" + ServerAPI.GET_CORPORATE_GROUPIDS) Response getCorporateGroupIdPrefix(@PathParam("name") final String organizationId); @POST @Path("/{name}" + ServerAPI.GET_CORPORATE_GROUPIDS) Response addCorporateGroupIdPrefix(@Auth final DbCredential credential, @PathParam("name") final String organizationId, final String corporateGroupId); @DELETE @Path("/{name}" + ServerAPI.GET_CORPORATE_GROUPIDS) Response removeCorporateGroupIdPrefix(@Auth final DbCredential credential, @PathParam("name") final String organizationId, final String corporateGroupId);  }
@Test public void deleteAnOrganization(){ final DbOrganization dbOrganization= new DbOrganization(); dbOrganization.setName("organization1"); dbOrganization.getCorporateGroupIdPrefixes().add("org.test"); when(repositoryHandler.getOrganization(dbOrganization.getName())).thenReturn(dbOrganization); client().addFilter(new HTTPBasicAuthFilter(GrapesTestUtils.USER_4TEST, GrapesTestUtils.PASSWORD_4TEST)); WebResource resource = client().resource("/" + ServerAPI.ORGANIZATION_RESOURCE + "/" + dbOrganization.getName()); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(repositoryHandler, times(1)).deleteOrganization(captor.capture()); assertEquals(dbOrganization.getName(), captor.getValue()); }
@Test public void removeCorporateGroupId(){ final DbOrganization dbOrganization= new DbOrganization(); dbOrganization.setName("organization1"); dbOrganization.getCorporateGroupIdPrefixes().add("org.test"); ArgumentCaptor<DbOrganization> captor = ArgumentCaptor.forClass(DbOrganization.class); when(repositoryHandler.getOrganization(dbOrganization.getName())).thenReturn(dbOrganization); client().addFilter(new HTTPBasicAuthFilter(GrapesTestUtils.USER_4TEST, GrapesTestUtils.PASSWORD_4TEST)); WebResource resource = client().resource("/" + ServerAPI.ORGANIZATION_RESOURCE + "/" + dbOrganization.getName() + ServerAPI.GET_CORPORATE_GROUPIDS); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class, "org.test"); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); verify(repositoryHandler).store((DbOrganization)captor.capture()); assertFalse(captor.getValue().getCorporateGroupIdPrefixes().contains("org.test")); }
@Test public void removeCorporateGroupIdsButCorporateGroupIdIsMissing(){ final DbOrganization dbOrganization= new DbOrganization(); dbOrganization.setName("organization1"); dbOrganization.getCorporateGroupIdPrefixes().add("org.test"); when(repositoryHandler.getOrganization(dbOrganization.getName())).thenReturn(dbOrganization); client().addFilter(new HTTPBasicAuthFilter(GrapesTestUtils.USER_4TEST, GrapesTestUtils.PASSWORD_4TEST)); WebResource resource = client().resource("/" + ServerAPI.ORGANIZATION_RESOURCE + "/" + dbOrganization.getName() + ServerAPI.GET_CORPORATE_GROUPIDS); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.BAD_REQUEST_400, response.getStatus()); }
@Test public void checkAuthenticationOnPostAndDeleteMethods(){ WebResource resource = client().resource("/" + ServerAPI.ORGANIZATION_RESOURCE); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus()); resource = client().resource("/" + ServerAPI.ORGANIZATION_RESOURCE + "/organization1"); response = resource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus()); resource = client().resource("/" + ServerAPI.ORGANIZATION_RESOURCE + "/organization1" + ServerAPI.GET_CORPORATE_GROUPIDS); response = resource.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus()); resource = client().resource("/" + ServerAPI.ORGANIZATION_RESOURCE + "/organization1" + ServerAPI.GET_CORPORATE_GROUPIDS); response = resource.accept(MediaType.APPLICATION_JSON).delete(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus()); }