WebSearchResource extends AbstractResource { @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{searchWord}") public Response getSearchResult(@PathParam("searchWord") final String searchWord, @Context final UriInfo uriInfo) { if(searchWord.length() < 3 || searchWord.indexOf(" ") != -1) { return Response.status(Response.Status.NOT_ACCEPTABLE).build(); } SearchView view = new SearchView(); final FiltersHolder filters = new FiltersHolder(); filters.init(uriInfo.getQueryParameters()); DbSearch result = getSearchHandler().getSearchResult(searchWord, filters); view.setSearchOj(result); return Response.ok(view).build(); } WebSearchResource(RepositoryHandler repoHandler, GrapesServerConfig dmConfig); @GET @Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON}) @Path("/{searchWord}") Response getSearchResult(@PathParam("searchWord") final String searchWord, @Context final UriInfo uriInfo);  }
@Test public void getSearchResult() throws Exception { List<String> moduleIds = new ArrayList<>(); moduleIds.add("testSearch_id_1"); moduleIds.add("testSearch_id_2"); List<String> artifactIds = new ArrayList<>(); artifactIds.add("testSearch_artifact_id_1"); artifactIds.add("testSearch_artifact_id_2"); DbSearch search = new DbSearch(); search.setModules(moduleIds); search.setArtifacts(artifactIds); when(repositoryHandler.getSearchResult(eq("testSearch"), (FiltersHolder) anyObject())).thenReturn(search); final WebResource resource = client().resource("/" + ServerAPI.SEARCH_RESOURCE + "/testSearch"); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final String results = response.getEntity(new GenericType<String>() { }); assertEquals("{\"modules\":[\"testSearch_id_1\",\"testSearch_id_2\"],\"artifacts\":[\"testSearch_artifact_id_1\",\"testSearch_artifact_id_2\"]}", results); }
@Test public void getNullSearchResult() { DbSearch search = new DbSearch(); when(repositoryHandler.getSearchResult(eq("testSearch"), (FiltersHolder) anyObject())).thenReturn(search); final WebResource resource = client().resource("/" + ServerAPI.SEARCH_RESOURCE + "/testSearch"); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final String results = response.getEntity(new GenericType<String>() { }); assertEquals("{\"modules\":null,\"artifacts\":null}", results); }
@Test public void getModulesSearchResult() { DbSearch search = new DbSearch(); List<String> moduleIds = new ArrayList<>(); moduleIds.add("testSearch_id_1"); moduleIds.add("testSearch_id_2"); search.setModules(moduleIds); when(repositoryHandler.getSearchResult(eq("testSearch"), (FiltersHolder) anyObject())).thenReturn(search); final WebResource resource = client().resource("/" + ServerAPI.SEARCH_RESOURCE + "/testSearch"); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final String results = response.getEntity(new GenericType<String>() { }); assertEquals("{\"modules\":[\"testSearch_id_1\",\"testSearch_id_2\"],\"artifacts\":null}", results); }
@Test public void getArtifactsSearchResult() { DbSearch search = new DbSearch(); List<String> artifactIds = new ArrayList<>(); artifactIds.add("testSearch_artifact_id_1"); artifactIds.add("testSearch_artifact_id_2"); search.setArtifacts(artifactIds); when(repositoryHandler.getSearchResult(eq("testSearch"), (FiltersHolder) anyObject())).thenReturn(search); final WebResource resource = client().resource("/" + ServerAPI.SEARCH_RESOURCE + "/testSearch"); final ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertNotNull(response); assertEquals(HttpStatus.OK_200, response.getStatus()); final String results = response.getEntity(new GenericType<String>() { }); assertEquals("{\"modules\":null,\"artifacts\":[\"testSearch_artifact_id_1\",\"testSearch_artifact_id_2\"]}", results); }