From f92d32193545e0b2cd8f9124b01433678b813435 Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Thu, 15 Apr 2021 12:15:01 +0100 Subject: Fix "ends-with" query syntax to conform with xPath definition "ends-with" is HOW we resolve it in sql query. 'descendant anywhere' is the correct Path name for the '//' operator - Updated method names, variable names, test description to reflect the correct terminolgy - Udpated query to always perfix the target (descendant name) with an '\' so it alwasy only matches whole node names - Updated regex for cpsPath to NOT accept triple /// (as per xPath this is invalid since a ndoeName cannot start with or contain a node separator Issue-ID: CPS-336 Signed-off-by: ToineSiebelink Change-Id: I9f181d6986d038311b839e3f9c5afc4237c7d347 --- .../org/onap/cps/spi/impl/CpsDataPersistenceServiceImpl.java | 2 +- .../src/main/java/org/onap/cps/spi/query/CpsPathQuery.java | 10 +++++----- .../main/java/org/onap/cps/spi/query/CpsPathQueryType.java | 4 ++-- .../java/org/onap/cps/spi/repository/FragmentRepository.java | 12 +++++++----- 4 files changed, 15 insertions(+), 13 deletions(-) (limited to 'cps-ri/src/main') 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 26aa4ac0b..ca279f30c 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 @@ -145,7 +145,7 @@ public class CpsDataPersistenceServiceImpl implements CpsDataPersistenceService .getLeafName(), cpsPathQuery.getLeafValue()); } else { fragmentEntities = fragmentRepository - .getByAnchorAndEndsWithXpath(anchorEntity.getId(), cpsPathQuery.getEndsWith()); + .getByAnchorAndXpathEndsInDescendantName(anchorEntity.getId(), cpsPathQuery.getDescendantName()); } return fragmentEntities.stream() .map(fragmentEntity -> toDataNode(fragmentEntity, fetchDescendantsOption)) diff --git a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java index 97a304d76..ce78c06d4 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQuery.java @@ -35,7 +35,7 @@ public class CpsPathQuery { private String xpathPrefix; private String leafName; private Object leafValue; - private String endsWith; + private String descendantName; private static final String NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS = "((?:\\/[^\\/]+){1,99})"; @@ -45,7 +45,7 @@ public class CpsPathQuery { private static final Pattern QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN = Pattern.compile(NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS + YANG_LEAF_VALUE_EQUALS_CONDITION); - private static final Pattern QUERY_CPS_PATH_ENDS_WITH_PATTERN = Pattern.compile("\\/\\/(.+)"); + private static final Pattern DESCENDANT_ANYWHERE_PATTERN = Pattern.compile("\\/\\/([^\\/].+)"); private static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+"); @@ -67,10 +67,10 @@ public class CpsPathQuery { cpsPathQuery.setLeafValue(convertLeafValueToCorrectType(matcher.group(3), cpsPath)); return cpsPathQuery; } - matcher = QUERY_CPS_PATH_ENDS_WITH_PATTERN.matcher(cpsPath); + matcher = DESCENDANT_ANYWHERE_PATTERN.matcher(cpsPath); if (matcher.matches()) { - cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_ENDS_WITH); - cpsPathQuery.setEndsWith(matcher.group(1)); + cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE); + cpsPathQuery.setDescendantName(matcher.group(1)); return cpsPathQuery; } throw new CpsPathException("Invalid cps path.", diff --git a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java index 1a0f8cabc..abdbfb9fd 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/query/CpsPathQueryType.java @@ -24,9 +24,9 @@ package org.onap.cps.spi.query; */ public enum CpsPathQueryType { /** - * Xpath ends with cps path query type e.g. //cps-path . + * Xpath descendant anywhere type e.g. //nodeName . */ - XPATH_ENDS_WITH, + XPATH_HAS_DESCENDANT_ANYWHERE, /** * Xpath leaf value cps path query type e.g. /cps-path[@leafName=leafValue] . */ diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index 5ff7cfc97..eb2e82b6c 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nordix Foundation. All rights reserved. + * Copyright (C) 2020-201 Nordix Foundation. All rights reserved. * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -61,7 +61,9 @@ public interface FragmentRepository extends JpaRepository List getByAnchorAndXpathAndLeafAttributes(@Param("anchor") int anchorId, @Param("xpath") String xpathPrefix, @Param("leafName") String leafName, @Param("leafValue") Object leafValue); - @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE %:xpath", nativeQuery = true) - // Above query will match the end of an xpath and anchor id - List getByAnchorAndEndsWithXpath(@Param("anchor") int anchorId, @Param("xpath") String xpath); -} \ No newline at end of file + @Query(value = "SELECT * FROM FRAGMENT WHERE anchor_id = :anchor AND xpath LIKE CONCAT('%/',:descendantName)", + nativeQuery = true) + // Above query will match the anchor id and last descendant name + List getByAnchorAndXpathEndsInDescendantName(@Param("anchor") int anchorId, + @Param("descendantName") String descendantName); +} -- cgit 1.2.3-korg