ModuleResource extends AbstractResource { @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}/{version}") public Response get(@PathParam("name") final String name, @PathParam("version") final String version) { LOG.info("Got a get module request."); final ModuleView view = new ModuleView(); final String moduleId = DbModule.generateID(name, version); final DbModule dbModule = getModuleHandler().getModule(moduleId); final Module module = getModelMapper().getModule(dbModule); view.setModule(module); view.setOrganization(dbModule.getOrganization()); return Response.ok(view).build(); } ModuleResource(final RepositoryHandler repoHandler, final GrapesServerConfig dmConfig); @POST @Produces(MediaType.TEXT_HTML) Response postModule(@Auth final DbCredential credential, final Module module); @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 getVersionsRedirection(@PathParam("name") final String name, @Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}" + ServerAPI.GET_VERSIONS) Response getVersions(@PathParam("name") final String name, @Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}/{version}") Response get(@PathParam("name") final String name, @PathParam("version") final String version); @DELETE @Path("/{name}/{version}") Response delete(@Auth final DbCredential credential, @PathParam("name") final String name, @PathParam("version") final String version); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}/{version}" + ServerAPI.GET_ORGANIZATION) Response getOrganization(@PathParam("name") final String name, @PathParam("version") final String version); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}/{version}" + ServerAPI.GET_ANCESTORS) Response getAncestors(@PathParam("name") final String name,
                                 @PathParam("version") final String version,
                                 @Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}/{version}" + ServerAPI.GET_DEPENDENCIES) Response getDependencies(@PathParam("name") final String name,
                                    @PathParam("version") final String version,
                                    @Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}/{version}" + ServerAPI.GET_DEPENDENCIES + ServerAPI.GET_REPORT) Response getDependencyReport(@PathParam("name") final String name,
                                        @PathParam("version") final String version,
                                        @Context final UriInfo uriInfo); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{name}/{version}" + ServerAPI.GET_LICENSES) Response getLicenses(@PathParam("name") final String name, @PathParam("version") final String version); @POST @Path("/{name}/{version}" + ServerAPI.PROMOTION) Response promote(@Auth final DbCredential credential, @PathParam("name") final String name, @PathParam("version") final String version); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{name}/{version}" + ServerAPI.PROMOTION + ServerAPI.GET_FEASIBLE) Response canBePromoted(@PathParam("name") final String name, @PathParam("version") final String version); @GET @Produces({MediaType.APPLICATION_JSON}) @Path("/{name}/{version}" + ServerAPI.PROMOTION + ServerAPI.GET_REPORT) Response getPromotionStatusReport(@PathParam("name") final String name,
                                             @PathParam("version") final String version,
                                             @QueryParam(ServerAPI.EXCLUDE_SNAPSHOT_PARAM) boolean excludeSnapshot); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{name}/{version}" + ServerAPI.PROMOTION) Response isPromoted(@PathParam("name") final String name, @PathParam("version") final String version); @GET @Produces(MediaType.APPLICATION_JSON) @Path("/{name}/{version}" + ServerAPI.GET_BUILD_INFO) Response getBuildInfo(@PathParam("name") final String name, @PathParam("version") final String version); @POST @Path("/{name}/{version}" + ServerAPI.GET_BUILD_INFO) Response updateBuildInfo(@PathParam("name") final String name, @PathParam("version") final String version, final Map<String, String> buildInfo); @GET @Produces(MediaType.APPLICATION_JSON) @Path(ServerAPI.GET_ALL) Response getAllModules(@Context final UriInfo uriInfo);  }
@Test public void getModule() { final DbModule dbModule = new DbModule(); dbModule.setName("moduleTest"); dbModule.setVersion("1.0.0"); when(repositoryHandler.getModule(dbModule.getId())).thenReturn(dbModule); final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/" + dbModule.getName() + "/" + dbModule.getVersion()); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final Module results = response.getEntity(Module.class); assertNotNull(results); assertEquals(dbModule.getName(), results.getName()); assertEquals(dbModule.getVersion(), results.getVersion()); }
@Test public void getAllModulesWithNoFilteringIsBadRequest() { final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + ServerAPI.GET_ALL); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.BAD_REQUEST_400, response.getStatus()); final String entity = response.getEntity(String.class); assertTrue(entity.contains("provide at least one module filtering criteria")); }
@Test public void getAllModulesNameFilter() { final DbModule dbModule = new DbModule(); dbModule.setName("moduleTest"); dbModule.setVersion("1.0.0"); when(repositoryHandler.getModules((FiltersHolder) anyObject())).thenReturn(Collections.singletonList(dbModule)); WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + ServerAPI.GET_ALL); resource = resource.queryParam("name", dbModule.getName()); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final List<Module> results = response.getEntity(new GenericType<List<Module>>() { }); assertNotNull(results); assertEquals(1, results.size()); }
@Test public void getModuleDependencies() { final DbModule dbModule = new DbModule(); dbModule.setName("moduleTest"); dbModule.setVersion("1.0.0"); final DbArtifact dbArtifact = new DbArtifact(); dbArtifact.setGroupId(GrapesTestUtils.CORPORATE_GROUPID_4TEST); dbArtifact.setArtifactId("artifact1"); dbArtifact.setVersion("1.2.3"); dbModule.addDependency(dbArtifact.getGavc(), Scope.COMPILE); when(repositoryHandler.getModule(dbModule.getId())).thenReturn(dbModule); when(repositoryHandler.getArtifact(dbArtifact.getGavc())).thenReturn(dbArtifact); final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/" + dbModule.getName() + "/" + dbModule.getVersion() + ServerAPI.GET_DEPENDENCIES); final ClientResponse response = resource.queryParam(ServerAPI.SCOPE_COMPILE_PARAM, "true") .queryParam(ServerAPI.SHOW_THIRPARTY_PARAM, "true") .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final List<Dependency> results = response.getEntity(new GenericType<List<Dependency>>() { }); assertNotNull(results); assertEquals(1, results.size()); assertEquals(dbArtifact.getGavc(), results.get(0).getTarget().getGavc()); assertEquals(Scope.COMPILE, results.get(0).getScope()); assertEquals(dbModule.getName(), results.get(0).getSourceName()); assertEquals(dbModule.getVersion(), results.get(0).getSourceVersion()); }
@Test public void getModuleDependencyReport() { final DbModule dbModule = new DbModule(); dbModule.setName("moduleTest"); dbModule.setVersion("1.0.0"); when(repositoryHandler.getModule(dbModule.getId())).thenReturn(dbModule); final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/" + dbModule.getName() + "/" + dbModule.getVersion() + ServerAPI.GET_DEPENDENCIES + ServerAPI.GET_REPORT); final ClientResponse response = resource .queryParam(ServerAPI.SCOPE_COMPILE_PARAM, "true") .queryParam(ServerAPI.SHOW_THIRPARTY_PARAM, "true") .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); }
@Test public void getPromotionStatusReport2NoLongerAvailable() { final DbModule dbModule = new DbModule(); dbModule.setName("moduleTest"); dbModule.setVersion("1.0.0-SNAPSHOT"); when(repositoryHandler.getModule(dbModule.getId())).thenReturn(dbModule); final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/" + dbModule.getName() + "/" + dbModule.getVersion() + ServerAPI.PROMOTION + "/report2"); final ClientResponse response = resource .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }
@Test public void getModuleOrganization() { final DbModule dbModule = new DbModule(); dbModule.setName("moduleTest"); dbModule.setVersion("1.0.0"); dbModule.setOrganization(GrapesTestUtils.ORGANIZATION_NAME_4TEST); when(repositoryHandler.getModule(dbModule.getId())).thenReturn(dbModule); final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/" + dbModule.getName() + "/" + dbModule.getVersion() + "/" + ServerAPI.ORGANIZATION_RESOURCE); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final Organization gotOrganization = response.getEntity(Organization.class); assertNotNull(gotOrganization); assertEquals(GrapesTestUtils.ORGANIZATION_NAME_4TEST, gotOrganization.getName()); }
@Test public void checkRedirectionOnGetModuleWithoutVersion() { final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/moduleName"); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.SEE_OTHER_303, response.getStatus()); }
@Test public void getBuildInfoOnModuleThatDoesNotExist() { final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/doesNotExist/doesNotExist" + ServerAPI.GET_BUILD_INFO); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }
@Test public void postBuildInfoOnModuleThatDoesNotExist() { final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/doesNotExist/doesNotExist" + ServerAPI.GET_BUILD_INFO); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.NOT_FOUND_404, response.getStatus()); }
@Test public void getDocumentation() { final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE); final ClientResponse response = resource.type(MediaType.TEXT_HTML).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); }
@Test public void getAllModuleNames() throws UnknownHostException { when(repositoryHandler.getModuleNames((FiltersHolder) anyObject())).thenReturn(Lists.newArrayList("module1")); final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + ServerAPI.GET_NAMES); final 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(1, results.size()); assertEquals("module1", results.get(0)); }
@Test public void getModuleVersions() throws UnknownHostException { final String moduleName = "moduleTest"; final String moduleVersion = "1.2.3-4"; when(repositoryHandler.getModuleVersions(eq(moduleName), (FiltersHolder) anyObject())).thenReturn(Lists.newArrayList(moduleVersion)); final WebResource resource = client().resource("/" + ServerAPI.MODULE_RESOURCE + "/" + moduleName + ServerAPI.GET_VERSIONS); final 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(1, results.size()); assertEquals(moduleVersion, results.get(0)); }