LicenseResource extends AbstractResource { @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}") public Response get(@PathParam("name") final String name){ LOG.info("Got a get license request."); final LicenseView view = new LicenseView(); final DbLicense dbLicense = getLicenseHandler().getLicense(name); final License license = getModelMapper().getLicense(dbLicense); view.setLicense(license); return Response.ok(view).build(); } LicenseResource(final RepositoryHandler repoHandler, final GrapesServerConfig dmConfig); @POST Response postLicense(@Auth final DbCredential credential, final License license); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path(ServerAPI.GET_NAMES) Response getNames(@Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}") Response get(@PathParam("name") final String name); @DELETE @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}") Response delete(@Auth final DbCredential credential, @PathParam("name") final String name); @POST @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}") Response approve(@Auth final DbCredential credential, @PathParam("name") final String name, @QueryParam(ServerAPI.APPROVED_PARAM) final BooleanParam approved);  }
@Test public void getDocumentation(){ WebResource resource = client().resource("/" + ServerAPI.LICENSE_RESOURCE); ClientResponse response = resource.type(MediaType.TEXT_HTML).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); }
@Test public void getAllLicenseNames() throws UnknownHostException{ final List<String> names = new ArrayList<String>(); names.add("licenseId"); when(repositoryHandler.getLicenseNames((FiltersHolder) anyObject())).thenReturn(names); WebResource resource = client().resource("/" + ServerAPI.LICENSE_RESOURCE + ServerAPI.GET_NAMES); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); List<?> licenses = (List<?>)response.getEntity(List.class); assertNotNull(licenses); assertEquals(1, licenses.size()); assertEquals("licenseId", licenses.get(0)); }
@Test public void getALicense() throws UnknownHostException{ DbLicense license = new DbLicense(); license.setName("license"); license.setLongName("longName"); license.setComments("bla"); license.setRegexp("gdfg"); license.setUrl("www.somewhere.org"); license.setApproved(true); when(repositoryHandler.getLicense(license.getName())).thenReturn(license); WebResource resource = client().resource("/" + ServerAPI.LICENSE_RESOURCE + "/" + license.getName()); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); License dbLicense = response.getEntity(License.class); assertNotNull(dbLicense); assertEquals(license.getName(), dbLicense.getName()); assertEquals(license.getLongName(), dbLicense.getLongName()); assertEquals(license.getComments(), dbLicense.getComments()); assertEquals(license.getRegexp(), dbLicense.getRegexp()); assertEquals(license.getUrl(), dbLicense.getUrl()); assertEquals(license.isApproved(), dbLicense.isApproved()); }
@Test public void getWrongLicense() throws UnknownHostException{ WebResource resource = client().resource("/" + ServerAPI.LICENSE_RESOURCE + "/license"); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }