From 56249583ba84956886454ec0b276c743161337ac Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Tue, 9 Mar 2021 15:27:06 +0000 Subject: Fix SOnarQube bug on Regex - Regex included unlimitted repetition is now limited to 99 99 nested yang container should sufice, in my experience in 5G 20-30 levels is the max I have seen we can always record it as a 'known limitation' -tried to improve the redability of the Regex using constant names - Added edge-case senarios testing related to query regex Issue-ID: CPS-89 Signed-off-by: ToineSiebelink Change-Id: Ic3442bc8ca22fe66976948f9913e033245a0e41c --- .../src/main/java/org/onap/cps/spi/query/CpsPathQuery.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'cps-ri/src/main/java') 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 54a6a96c4e..7b9bfba174 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 @@ -37,14 +37,18 @@ public class CpsPathQuery { private Object leafValue; private String endsWith; - public static final Pattern QUERY_CPS_PATH_WITH_SINGLE_LEAF_PATTERN = - Pattern.compile("((?:\\/[^\\/]+)+?)\\[\\s*@(\\S+?)\\s*=\\s*(.*?)\\s*\\]"); + private static final String NON_CAPTURING_GROUP_1_TO_99_YANG_CONTAINERS = "((?:\\/[^\\/]+){1,99})"; - public static final Pattern QUERY_CPS_PATH_ENDS_WITH_PATTERN = Pattern.compile("\\/\\/(.+)"); + private static final String YANG_LEAF_VALUE_EQUALS_CONDITION = "\\[\\s*@(\\S+?)\\s*=\\s*(.*?)\\s*\\]"; - public static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+"); + 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); - public static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]"); + private static final Pattern QUERY_CPS_PATH_ENDS_WITH_PATTERN = Pattern.compile("\\/\\/(.+)"); + + private static final Pattern LEAF_INTEGER_VALUE_PATTERN = Pattern.compile("[-+]?\\d+"); + + private static final Pattern LEAF_STRING_VALUE_PATTERN = Pattern.compile("['\"](.*)['\"]"); /** * Returns a cps path query. -- cgit 1.2.3-korg