use of ca.corefacility.bioinformatics.irida.util.TreeNode in project irida by phac-nml.
the class ProjectsControllerTest method testSearchTaxonomy.
@Test
public void testSearchTaxonomy() {
String searchTerm = "bac";
TreeNode<String> root = new TreeNode<>("Bacteria");
TreeNode<String> child = new TreeNode<>("ChildBacteria");
child.setParent(root);
root.addChild(child);
List<TreeNode<String>> resultList = new ArrayList<>();
resultList.add(root);
// the elements that should be at the root
List<String> results = Lists.newArrayList(searchTerm, "Bacteria");
when(taxonomyService.search(searchTerm)).thenReturn(resultList);
List<Map<String, Object>> searchTaxonomy = controller.searchTaxonomy(searchTerm);
verify(taxonomyService).search(searchTerm);
assertFalse(searchTaxonomy.isEmpty());
assertEquals(2, searchTaxonomy.size());
for (Map<String, Object> element : searchTaxonomy) {
assertTrue(element.containsKey("text"));
assertTrue(element.containsKey("id"));
assertTrue(results.contains(element.get("text")));
}
}
use of ca.corefacility.bioinformatics.irida.util.TreeNode in project irida by phac-nml.
the class InMemoryTaxonomyService method search.
/**
* {@inheritDoc}
*/
@Override
public Collection<TreeNode<String>> search(String searchTerm) {
HashMap<String, TreeNode<String>> visited = new HashMap<>();
Set<TreeNode<String>> visitedRoots = new HashSet<>();
if (!Strings.isNullOrEmpty(searchTerm)) {
ParameterizedSparqlString query = new ParameterizedSparqlString(LABEL_SEARCH_QUERY);
query.setLiteral("term", QueryParser.escape(searchTerm));
query.setIri("parent", ROOT_IRI);
Query q = query.asQuery();
QueryExecution qexec = QueryExecutionFactory.create(q, dataset);
ResultSet result = qexec.execSelect();
while (result.hasNext()) {
QuerySolution next = result.next();
buildTrimmedResultTree(next.getResource("s"), searchTerm, visited);
}
// get all the roots
for (Entry<String, TreeNode<String>> entry : visited.entrySet()) {
TreeNode<String> current = entry.getValue();
while (current.getParent() != null) {
current = current.getParent();
}
if (!visitedRoots.contains(current)) {
visitedRoots.add(current);
}
}
}
return visitedRoots;
}
Aggregations