QueryBuilder { public BasicDBObject getSearchQuery(final List<String> searchWords, final Coordinates coordinates, final String[] searchCategories) { final List<BasicDBObject> searchParameters = new ArrayList<>(); for (int i = 0; i < searchWords.size(); i++) { String key = searchWords.get(i); if (!key.equals("")) { Pattern matchPattern; try { matchPattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE); } catch (final PatternSyntaxException e) { key = Pattern.quote(key); searchWords.set(i, key); matchPattern = Pattern.compile(key); } for (final String searchCategory : searchCategories) { searchParameters.add(new BasicDBObject(searchCategory, matchPattern)); } } } return coordinates.getQueryObject().append("$or", searchParameters); } private  QueryBuilder(); static QueryBuilder getInstance(); BasicDBObject getSearchQuery(final List<String> searchWords, final Coordinates coordinates, final String[] searchCategories); BasicDBObject buildFilterQuery(final Coordinates coordinates, final String searchText,
			final Integer viewPassed, final Integer viewFailed, final Integer viewUndefined,
			final Integer viewSkipped, final String start); List<DBObject> buildHasTagsQuery();  }
@Test public void buildTestQueryWithEmptySearch() { final String[] searchCategories = { "name", "age" }; final List<String> searchKeys = new ArrayList<>(); searchKeys.add(""); final BasicDBObject searchQuery = this.queryBuilder.getSearchQuery(searchKeys, this.coordinates, searchCategories); @SuppressWarnings("unchecked") final List<DBObject> queryResults = (ArrayList<DBObject>) searchQuery.get("$or"); assertTrue(queryResults.isEmpty()); }
@Test public void buildSimpleTestQuery() { final String[] searchCategories = { "name", "age" }; final List<String> searchKeys = new ArrayList<>(); searchKeys.add("hi"); searchKeys.add("there"); final BasicDBObject searchQuery = this.queryBuilder.getSearchQuery(searchKeys, this.coordinates, searchCategories); @SuppressWarnings("unchecked") final List<DBObject> queryResults = (ArrayList<DBObject>) searchQuery.get("$or"); assertEquals("{\"name\": {\"$regex\": \"hi\", \"$options\": \"i\"}}", queryResults.get(0).toString()); assertEquals("{\"age\": {\"$regex\": \"hi\", \"$options\": \"i\"}}", queryResults.get(1).toString()); assertEquals("{\"name\": {\"$regex\": \"there\", \"$options\": \"i\"}}", queryResults.get(2).toString()); assertEquals("{\"age\": {\"$regex\": \"there\", \"$options\": \"i\"}}", queryResults.get(3).toString()); }