diff options
author | 2023-05-04 11:35:41 +0100 | |
---|---|---|
committer | 2023-05-04 12:51:30 +0100 | |
commit | d3a2b33cf99e1ae334c0690e7f5dc1a299f39fb7 (patch) | |
tree | 332ae5a28ac61c13cd1cd90bbfe935643052de63 | |
parent | 08a47195fb3882e396b2dd01b01afa5da42255fb (diff) |
Fix for quickfind with descendants incl. list entries
- add specialized regex
- fixed legacy issues with absolute paths (added ^ in regex's)
- split into 2 methods for normal queries and quickfind
- fixed order of private methods
Issue-ID: CPS-1671
Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Change-Id: I785a230cd0e3e0359e3cbb0fa754a21b6e2be7ff
3 files changed, 55 insertions, 24 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java index 95270786f..78983ee0d 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java @@ -357,7 +357,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService 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 = (anchorEntity == ALL_ANCHORS) ? fragmentRepository.quickFindWithDescendantsAcrossAnchor(xpathRegex) : fragmentRepository.quickFindWithDescendants(anchorEntity.getId(), xpathRegex); diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java index 212eb93cb..013487334 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentQueryBuilder.java @@ -41,10 +41,10 @@ import org.springframework.stereotype.Component; @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; @@ -78,15 +78,40 @@ public class FragmentQueryBuilder { return getQuery(cpsPathQuery, sqlStringBuilder, queryParameters); } + /** + * 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 + * @return a string representing the required regular expression + */ + 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 Query getQuery(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder, final Map<String, Object> queryParameters) { - final String xpathRegex = getXpathSqlRegex(cpsPathQuery, false); + final String xpathRegex = getXpathSqlRegex(cpsPathQuery); queryParameters.put("xpathRegex", xpathRegex); final List<String> queryBooleanOperatorsType = cpsPathQuery.getBooleanOperatorsType(); if (cpsPathQuery.hasLeafConditions()) { sqlStringBuilder.append(" AND ("); final Queue<String> booleanOperatorsQueue = (queryBooleanOperatorsType == null) ? null : new LinkedList<>( - queryBooleanOperatorsType); + queryBooleanOperatorsType); cpsPathQuery.getLeavesData().entrySet().forEach(entry -> { sqlStringBuilder.append(" attributes @> "); sqlStringBuilder.append("'" + jsonObjectMapper.asJsonString(entry) + "'"); @@ -103,27 +128,16 @@ public class FragmentQueryBuilder { return query; } - /** - * Create a regular expression (string) for xpath 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 - * @return a string representing the required regular expression - */ - public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery, final boolean includeDescendants) { + 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) { diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/functional/CpsQueryServiceIntegrationSpec.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/functional/CpsQueryServiceIntegrationSpec.groovy index b723d7d99..4e596da14 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/functional/CpsQueryServiceIntegrationSpec.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/functional/CpsQueryServiceIntegrationSpec.groovy @@ -298,4 +298,21 @@ class CpsQueryServiceIntegrationSpec extends FunctionalSpecBase { thrown(CpsPathException) } + def 'Cps Path querys 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_1, BOOKSTORE_ANCHOR_1, cpsPath, INCLUDE_ALL_DESCENDANTS) + then: 'correct number of datanodes are returned' + assert countDataNodesInTree(result) == expectedNumberOfDataNodes + where: + scenario | cpsPath || expectedNumberOfDataNodes + 'absolute path all list entries' | '/bookstore/categories' || 13 + 'absolute path 1 list entry by key' | '/bookstore/categories[@code="3"]' || 5 + 'absolute path 1 list entry by name' | '/bookstore/categories[@name="Comedy"]' || 5 + 'relative path all list entries' | '//categories' || 13 + 'relative path 1 list entry by key' | '//categories[@code="3"]' || 5 + 'relative path 1 list entry by leaf' | '//categories[@name="Comedy"]' || 5 + 'incomplete absolute path' | '/categories' || 0 + 'incomplete absolute 1 list entry' | '/categories[@code="3"]' || 0 + } + } |