@Test public void testBuild() { String param1 = "sourceSystem.code"; String value1 = "CRS"; String param2 = "collectionType"; String value2 = "Hymenoptera"; String param3 = "kindOfUnit"; String value3 = "WholeOrganism"; String param4 = "gatheringEvent.country"; String value4 = "Greece"; String param5 = "_logicalOperator"; String logicalOperatorStr = "AND"; String param6 = "_size"; String sizeStr = "10"; String param7 = "_from"; String fromStr = "25"; String param8 = "_fields"; String fieldsStr = "sourceSystemId,identifications.scientificName.fullScientificName"; String param9 = "_sortFields"; String sortFieldsStr = "id,identifications.scientificName.fullScientificName:DESC"; String param10 = "_ignoreCase"; String ignoreCaseStr = "true"; ComparisonOperator comparisonOperatorStr = EQUALS; if (isTrueValue(ignoreCaseStr)) { comparisonOperatorStr = EQUALS_IC; } MultivaluedHashMap<String, String> parameterMap = new MultivaluedHashMap<>(); parameterMap.put(param1, new ArrayList<>(Arrays.asList(value1))); parameterMap.put(param2, new ArrayList<>(Arrays.asList(value2))); parameterMap.put(param3, new ArrayList<>(Arrays.asList(value3))); parameterMap.put(param4, new ArrayList<>(Arrays.asList(value4))); parameterMap.put(param5, new ArrayList<>(Arrays.asList(logicalOperatorStr))); parameterMap.put(param6, new ArrayList<>(Arrays.asList(sizeStr))); parameterMap.put(param7, new ArrayList<>(Arrays.asList(fromStr))); parameterMap.put(param8, new ArrayList<>(Arrays.asList(fieldsStr))); parameterMap.put(param9, new ArrayList<>(Arrays.asList(sortFieldsStr))); parameterMap.put(param10, new ArrayList<>(Arrays.asList(ignoreCaseStr))); UriInfo uriInfo = mock(UriInfo.class); when(uriInfo.getQueryParameters()).thenReturn(parameterMap); QuerySpec qsActual = new HttpQuerySpecBuilder(uriInfo).build(); QuerySpec qsExpected = new QuerySpec(); QueryCondition cond1 = new QueryCondition(param1, comparisonOperatorStr, value1); QueryCondition cond2 = new QueryCondition(param2, comparisonOperatorStr, value2); QueryCondition cond3 = new QueryCondition(param3, comparisonOperatorStr, value3); QueryCondition cond4 = new QueryCondition(param4, comparisonOperatorStr, value4); qsExpected.addCondition(cond1); qsExpected.addCondition(cond2); qsExpected.addCondition(cond3); qsExpected.addCondition(cond4); qsExpected.setLogicalOperator(LogicalOperator.parse(logicalOperatorStr)); qsExpected.setSize(Integer.parseInt(sizeStr)); qsExpected.setFrom(Integer.parseInt(fromStr)); qsExpected.addFields(fieldsStr.split(",")); List<SortField> sortFields = new ArrayList<>(); for (String sortFieldStr : sortFieldsStr.split(",")) { if (sortFieldStr.indexOf(":") < 0) { sortFields.add(new SortField(sortFieldStr, SortOrder.ASC)); } else { sortFields.add(new SortField(sortFieldStr.split(":")[0], SortOrder.parse(sortFieldStr.split(":")[1]))); } } qsExpected.setSortFields(sortFields); assertTrue("Comparison of Human Readable and Complex Query Spec", HttpQuerySpecUtil.compareQuerySpecs(qsActual, qsExpected)); }
public QuerySpec build() { logger.info("Extracting QuerySpec object from request"); checkParams(uriInfo); List<String> values = params.get(PARAM_QUERY_SPEC); if (values != null) { return buildFromSearchSpecParam(values); } QuerySpec qs = new QuerySpec(); ComparisonOperator operator = getComparisonOperator(); for (String param : params.keySet()) { values = params.get(param); if (values.size() != 1) { String msg = String.format(ERR_DUPLICATE_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } String value = values.iterator().next(); logger.info("Processing parameter {}: \"{}\"", param, value); switch (param) { case "querySpec": throw new HTTP400Exception(uriInfo, ERR_NO_UNDERSCORE); case PARAM_IGNORE_CASE: break; case PARAM_SORT_FIELDS: qs.setSortFields(getSortFields(value)); break; case PARAM_FROM: qs.setFrom(getIntParam(param, value)); break; case PARAM_SIZE: qs.setSize(getIntParam(param, value)); break; case PARAM_OPERATOR: qs.setLogicalOperator(getLogicalOperator(value)); break; case PARAM_FIELDS: qs.setFields(getFields(value)); break; default: if (param.charAt(0) == '_') { String msg = String.format(ERR_ILLEGAL_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } if (value.equals("@NULL@")) { qs.addCondition(new QueryCondition(param, EQUALS, null)); } else if (value.equals("@NOT_NULL@")) { qs.addCondition(new QueryCondition(param, NOT_EQUALS, null)); } else { qs.addCondition(new QueryCondition(param, operator, value)); } break; } } return qs; }
HttpQuerySpecBuilder { public QuerySpec build() { logger.info("Extracting QuerySpec object from request"); checkParams(uriInfo); List<String> values = params.get(PARAM_QUERY_SPEC); if (values != null) { return buildFromSearchSpecParam(values); } QuerySpec qs = new QuerySpec(); ComparisonOperator operator = getComparisonOperator(); for (String param : params.keySet()) { values = params.get(param); if (values.size() != 1) { String msg = String.format(ERR_DUPLICATE_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } String value = values.iterator().next(); logger.info("Processing parameter {}: \"{}\"", param, value); switch (param) { case "querySpec": throw new HTTP400Exception(uriInfo, ERR_NO_UNDERSCORE); case PARAM_IGNORE_CASE: break; case PARAM_SORT_FIELDS: qs.setSortFields(getSortFields(value)); break; case PARAM_FROM: qs.setFrom(getIntParam(param, value)); break; case PARAM_SIZE: qs.setSize(getIntParam(param, value)); break; case PARAM_OPERATOR: qs.setLogicalOperator(getLogicalOperator(value)); break; case PARAM_FIELDS: qs.setFields(getFields(value)); break; default: if (param.charAt(0) == '_') { String msg = String.format(ERR_ILLEGAL_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } if (value.equals("@NULL@")) { qs.addCondition(new QueryCondition(param, EQUALS, null)); } else if (value.equals("@NOT_NULL@")) { qs.addCondition(new QueryCondition(param, NOT_EQUALS, null)); } else { qs.addCondition(new QueryCondition(param, operator, value)); } break; } } return qs; } }
HttpQuerySpecBuilder { public QuerySpec build() { logger.info("Extracting QuerySpec object from request"); checkParams(uriInfo); List<String> values = params.get(PARAM_QUERY_SPEC); if (values != null) { return buildFromSearchSpecParam(values); } QuerySpec qs = new QuerySpec(); ComparisonOperator operator = getComparisonOperator(); for (String param : params.keySet()) { values = params.get(param); if (values.size() != 1) { String msg = String.format(ERR_DUPLICATE_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } String value = values.iterator().next(); logger.info("Processing parameter {}: \"{}\"", param, value); switch (param) { case "querySpec": throw new HTTP400Exception(uriInfo, ERR_NO_UNDERSCORE); case PARAM_IGNORE_CASE: break; case PARAM_SORT_FIELDS: qs.setSortFields(getSortFields(value)); break; case PARAM_FROM: qs.setFrom(getIntParam(param, value)); break; case PARAM_SIZE: qs.setSize(getIntParam(param, value)); break; case PARAM_OPERATOR: qs.setLogicalOperator(getLogicalOperator(value)); break; case PARAM_FIELDS: qs.setFields(getFields(value)); break; default: if (param.charAt(0) == '_') { String msg = String.format(ERR_ILLEGAL_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } if (value.equals("@NULL@")) { qs.addCondition(new QueryCondition(param, EQUALS, null)); } else if (value.equals("@NOT_NULL@")) { qs.addCondition(new QueryCondition(param, NOT_EQUALS, null)); } else { qs.addCondition(new QueryCondition(param, operator, value)); } break; } } return qs; } HttpQuerySpecBuilder(UriInfo uriInfo);  HttpQuerySpecBuilder(MultivaluedMap<String, String> formData, UriInfo uriInfo); }
HttpQuerySpecBuilder { public QuerySpec build() { logger.info("Extracting QuerySpec object from request"); checkParams(uriInfo); List<String> values = params.get(PARAM_QUERY_SPEC); if (values != null) { return buildFromSearchSpecParam(values); } QuerySpec qs = new QuerySpec(); ComparisonOperator operator = getComparisonOperator(); for (String param : params.keySet()) { values = params.get(param); if (values.size() != 1) { String msg = String.format(ERR_DUPLICATE_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } String value = values.iterator().next(); logger.info("Processing parameter {}: \"{}\"", param, value); switch (param) { case "querySpec": throw new HTTP400Exception(uriInfo, ERR_NO_UNDERSCORE); case PARAM_IGNORE_CASE: break; case PARAM_SORT_FIELDS: qs.setSortFields(getSortFields(value)); break; case PARAM_FROM: qs.setFrom(getIntParam(param, value)); break; case PARAM_SIZE: qs.setSize(getIntParam(param, value)); break; case PARAM_OPERATOR: qs.setLogicalOperator(getLogicalOperator(value)); break; case PARAM_FIELDS: qs.setFields(getFields(value)); break; default: if (param.charAt(0) == '_') { String msg = String.format(ERR_ILLEGAL_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } if (value.equals("@NULL@")) { qs.addCondition(new QueryCondition(param, EQUALS, null)); } else if (value.equals("@NOT_NULL@")) { qs.addCondition(new QueryCondition(param, NOT_EQUALS, null)); } else { qs.addCondition(new QueryCondition(param, operator, value)); } break; } } return qs; } HttpQuerySpecBuilder(UriInfo uriInfo);  HttpQuerySpecBuilder(MultivaluedMap<String, String> formData, UriInfo uriInfo); QuerySpec build(); }
HttpQuerySpecBuilder { public QuerySpec build() { logger.info("Extracting QuerySpec object from request"); checkParams(uriInfo); List<String> values = params.get(PARAM_QUERY_SPEC); if (values != null) { return buildFromSearchSpecParam(values); } QuerySpec qs = new QuerySpec(); ComparisonOperator operator = getComparisonOperator(); for (String param : params.keySet()) { values = params.get(param); if (values.size() != 1) { String msg = String.format(ERR_DUPLICATE_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } String value = values.iterator().next(); logger.info("Processing parameter {}: \"{}\"", param, value); switch (param) { case "querySpec": throw new HTTP400Exception(uriInfo, ERR_NO_UNDERSCORE); case PARAM_IGNORE_CASE: break; case PARAM_SORT_FIELDS: qs.setSortFields(getSortFields(value)); break; case PARAM_FROM: qs.setFrom(getIntParam(param, value)); break; case PARAM_SIZE: qs.setSize(getIntParam(param, value)); break; case PARAM_OPERATOR: qs.setLogicalOperator(getLogicalOperator(value)); break; case PARAM_FIELDS: qs.setFields(getFields(value)); break; default: if (param.charAt(0) == '_') { String msg = String.format(ERR_ILLEGAL_PARAM, param); throw new HTTP400Exception(uriInfo, msg); } if (value.equals("@NULL@")) { qs.addCondition(new QueryCondition(param, EQUALS, null)); } else if (value.equals("@NOT_NULL@")) { qs.addCondition(new QueryCondition(param, NOT_EQUALS, null)); } else { qs.addCondition(new QueryCondition(param, operator, value)); } break; } } return qs; } HttpQuerySpecBuilder(UriInfo uriInfo);  HttpQuerySpecBuilder(MultivaluedMap<String, String> formData, UriInfo uriInfo); QuerySpec build(); static final String PARAM_QUERY_SPEC; static final String PARAM_FIELDS; static final String PARAM_FROM; static final String PARAM_SIZE; static final String PARAM_OPERATOR; static final String PARAM_SORT_FIELDS; static final String PARAM_IGNORE_CASE; }