final AnchorEntity anchorEntity,
final CpsPathQuery cpsPathQuery) {
Collection<FragmentEntity> fragmentEntities;
- final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegex(cpsPathQuery, true);
+ final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegexForQuickFindWithDescendants(cpsPathQuery);
final List<FragmentExtract> fragmentExtracts =
fragmentRepository.quickFindWithDescendants(anchorEntity.getId(), xpathRegex);
fragmentEntities = FragmentEntityArranger.toFragmentEntityTrees(anchorEntity, fragmentExtracts);
final FetchDescendantsOption fetchDescendantsOption, final DataspaceEntity dataspaceEntity,
final CpsPathQuery cpsPathQuery) {
Collection<FragmentEntity> fragmentEntities;
- final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegex(cpsPathQuery, true);
+ final String xpathRegex = FragmentQueryBuilder.getXpathSqlRegexForQuickFindWithDescendants(cpsPathQuery);
final List<FragmentExtract> fragmentExtracts = fragmentRepository
.quickFindWithDescendantsAcrossAnchor(dataspaceEntity.getId(), xpathRegex);
final Map<Long, AnchorEntity> fragmentExtractAnchorMap = getFragmentExtractAnchorMap(fragmentExtracts);
@Slf4j
@Component
public class FragmentQueryBuilder {
- private static final String REGEX_ABSOLUTE_PATH_PREFIX = ".*\\/";
- private static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?";
- private static final String REGEX_DESCENDANT_PATH_POSTFIX = "(\\/.*)?";
- private static final String REGEX_END_OF_INPUT = "$";
+ private static final String REGEX_ABSOLUTE_PATH_PREFIX = "^";
+ private static final String REGEX_DESCENDANT_PATH_PREFIX = "^.*\\/";
+ private static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?$";
+ private static final String REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS = "(\\[@.*?])?(\\/.*)?$";
@PersistenceContext
private EntityManager entityManager;
final Map<String, Object> queryParameters = new HashMap<>();
queryParameters.put("anchorId", anchorId);
sqlStringBuilder.append(" AND xpath ~ :xpathRegex");
- final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false);
+ final String xpathRegex = getXpathSqlRegex(cpsPathQuery);
queryParameters.put("xpathRegex", xpathRegex);
if (cpsPathQuery.hasLeafConditions()) {
sqlStringBuilder.append(" AND attributes @> :leafDataAsJson\\:\\:jsonb");
final StringBuilder sqlStringBuilder = new StringBuilder("SELECT * FROM FRAGMENT WHERE dataspace_id = "
+ ":dataspaceId AND xpath ~ :xpathRegex");
final Map<String, Object> queryParameters = new HashMap<>();
- final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false);
+ final String xpathRegex = getXpathSqlRegex(cpsPathQuery);
queryParameters.put("dataspaceId", dataspaceId);
queryParameters.put("xpathRegex", xpathRegex);
if (cpsPathQuery.hasLeafConditions()) {
}
/**
- * Create a regular expression (string) for xpath based on the given cps path query.
+ * Create a regular expression (string) for matching xpaths based on the given cps path query.
*
- * @param cpsPathQuery the cps path query to determine the required regular expression
- * @param includeDescendants include descendants yes or no
+ * @param cpsPathQuery the cps path query to determine the required regular expression
* @return a string representing the required regular expression
*/
- public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery, final boolean includeDescendants) {
+ public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery) {
+ final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery);
+ xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX);
+ return xpathRegexBuilder.toString();
+ }
+
+ /**
+ * Create a regular expression (string) for matching xpaths with (all) descendants
+ * based on the given cps path query.
+ *
+ * @param cpsPathQuery the cps path query to determine the required regular expression
+ * @return a string representing the required regular expression
+ */
+ public static String getXpathSqlRegexForQuickFindWithDescendants(final CpsPathQuery cpsPathQuery) {
+ final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery);
+ xpathRegexBuilder.append(REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS);
+ return xpathRegexBuilder.toString();
+ }
+
+ private static StringBuilder getRegexStringBuilderWithPrefix(final CpsPathQuery cpsPathQuery) {
final StringBuilder xpathRegexBuilder = new StringBuilder();
if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) {
- xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix()));
- } else {
xpathRegexBuilder.append(REGEX_ABSOLUTE_PATH_PREFIX);
- xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName()));
- }
- xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX);
- if (includeDescendants) {
- xpathRegexBuilder.append(REGEX_DESCENDANT_PATH_POSTFIX);
+ xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix()));
+ return xpathRegexBuilder;
}
- xpathRegexBuilder.append(REGEX_END_OF_INPUT);
- return xpathRegexBuilder.toString();
+ xpathRegexBuilder.append(REGEX_DESCENDANT_PATH_PREFIX);
+ xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName()));
+ return xpathRegexBuilder;
}
private static String escapeXpath(final String xpath) {
import org.onap.cps.integration.base.FunctionalSpecBase
import org.onap.cps.spi.FetchDescendantsOption
+import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS
+
class CpsQueryServiceIntegrationSpec extends FunctionalSpecBase {
def objectUnderTest
'the and condition is used' | '//books[@lang="English" and @price=15]' || 2 | [lang:"English", price:15]
'the and is used where result does not exist' | '//books[@lang="English" and @price=1000]' || 0 | []
}
+
+ def 'Cps Path queries with all descendants including descendants that are list entries: #scenario.'() {
+ when: 'a query is executed to get a data node by the given cps path'
+ def result = objectUnderTest.queryDataNodes(FUNCTIONAL_TEST_DATASPACE, BOOKSTORE_ANCHOR, cpsPath, INCLUDE_ALL_DESCENDANTS)
+ then: 'correct number of datanodes are returned'
+ assert countDataNodesInTree(result) == expectedNumberOfDataNodes
+ where: 'the following cps paths are used'
+ scenario | cpsPath || expectedNumberOfDataNodes
+ 'absolute path all list entries' | '/bookstore/categories' || 7
+ 'absolute path 1 list entry by key' | '/bookstore/categories[@code="3"]' || 2
+ 'absolute path 1 list entry by leaf' | '/bookstore/categories[@name="Comedy"]' || 2
+ 'relative path all list entries' | '//categories' || 7
+ 'relative path 1 list entry by key' | '//categories[@code="3"]' || 2
+ 'relative path 1 list entry by leaf' | '//categories[@name="Comedy"]' || 2
+ 'incomplete absolute path' | '/categories' || 0
+ 'incomplete absolute path list entry' | '/categories[@code="3"]' || 0
+ }
}