ProductResource 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 product request."); final DbProduct dbProduct = getProductHandler().getProduct(name); final ProductView view = new ProductView(dbProduct); return Response.ok(view).build(); } ProductResource(final RepositoryHandler repositoryHandler, final GrapesServerConfig configuration); @POST Response createProduct(@Auth final DbCredential credential, final String productName); @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 @Path("/{name}" + ServerAPI.GET_MODULES) @Produces(MediaType.APPLICATION_JSON) Response getModuleNames(@PathParam("name") final String name); @POST @Path("/{name}" + ServerAPI.GET_MODULES) Response setModuleNames(@Auth final DbCredential credential, @PathParam("name") final String name, final List<String> moduleNames); @GET @Path("/{name}" + ServerAPI.GET_DELIVERIES) @Produces(MediaType.APPLICATION_JSON) Response getDeliveries(@PathParam("name") final String name); @POST @Path("/{name}" + ServerAPI.GET_DELIVERIES) Response createNewDelivery(@Auth final DbCredential credential, @PathParam("name") final String name, final Delivery delivery); @GET @Path("/{name}" + ServerAPI.GET_DELIVERIES+"/{commercialName}"+"/{commercialVersion}") @Produces(MediaType.APPLICATION_JSON) Response getDelivery(@PathParam("name") final String name, @PathParam("commercialName") final String commercialName, @PathParam("commercialVersion") final String commercialVersion); @DELETE @Path("/{name}" + ServerAPI.GET_DELIVERIES+"/{commercialName}"+"/{commercialVersion}") @Produces(MediaType.APPLICATION_JSON) Response deleteDelivery(@Auth final DbCredential credential, @PathParam("name") final String name, @PathParam("commercialName") final String commercialName, @PathParam("commercialVersion") final String commercialVersion); @POST @Path("/{name}" + ServerAPI.GET_DELIVERIES+"/{commercialName}"+"/{commercialVersion}") @Produces(MediaType.APPLICATION_JSON) Response setDeliveryDependency(@Auth final DbCredential credential,
                                          @PathParam("name") final String name,
                                          @PathParam("commercialName") final String commercialName,
                                          @PathParam("commercialVersion") final String commercialVersion,
                                          List<String> dependencies);  }
@Test public void getDocumentation(){ WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE); ClientResponse response = resource.type(MediaType.TEXT_HTML).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); }
@Test public void getAllProductNames(){ final List<String> names = Lists.newArrayList("product1", "product2", "product3"); when(repositoryHandler.getProductNames()).thenReturn(names); WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE + ServerAPI.GET_NAMES); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); List<?> productNames = (List<?>)response.getEntity(ArrayList.class); assertNotNull(productNames); assertEquals(3, productNames.size()); }
@Test public void getAProduct(){ final DbProduct product = new DbProduct(); product.setName("product1"); when(repositoryHandler.getProduct(product.getName())).thenReturn(product); WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE + "/" + product.getName()); ClientResponse response = resource.accept(MediaType.TEXT_HTML).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); }
@Test public void getProductModules(){ final DbProduct product = new DbProduct(); product.setName("product1"); product.setModules(Lists.newArrayList("module1", "module2", "module3")); when(repositoryHandler.getProduct(product.getName())).thenReturn(product); WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE + "/" + product.getName() + ServerAPI.GET_MODULES); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final List<String> results = response.getEntity(new GenericType<List<String>>() { }); assertNotNull(results); assertEquals(3, results.size()); }
@Test public void getProductModulesOfAProductThatDoesNotExist(){ WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE + "/doesNotExist" + ServerAPI.GET_MODULES); ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }
@Test public void getDeliveriesOfAProductThatDoesNotExist(){ WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE + "/doesNotExist" + ServerAPI.GET_DELIVERIES); ClientResponse response = resource.type(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }
@Test public void getDeliveryOfAProductThatDoesNotExist(){ WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE + "/doesNotExist" + ServerAPI.GET_DELIVERIES+ "/delivery1/1.0.0"); ClientResponse response = resource.type(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }
@Test public void getDeliveryThatDoesNotExistOnAProductThatExist(){ final DbProduct product = new DbProduct(); product.setName("product1"); when(repositoryHandler.getProduct(product.getName())).thenReturn(product); WebResource resource = client().resource("/" + ServerAPI.PRODUCT_RESOURCE + "/" + product.getName() + ServerAPI.GET_DELIVERIES+ "/doesNotExist/1.0.0"); ClientResponse response = resource.type(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }