diff options
author | Toine Siebelink <toine.siebelink@est.tech> | 2023-01-23 12:07:27 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2023-01-23 12:07:27 +0000 |
commit | c6bebbcfc4dbef5e91245b5f69714b238c0515af (patch) | |
tree | 371fa2e99edb452c0b07841a8c962b4640f95ac1 /cps-service/src | |
parent | 1014475eea7c67a6fd6caa63ca192ea23a23ddd3 (diff) | |
parent | cc21a025308153e8f187cd3e82cf828191d7b387 (diff) |
Merge "Added depth parameter in query nodes API."
Diffstat (limited to 'cps-service/src')
3 files changed, 57 insertions, 2 deletions
diff --git a/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java b/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java index b80054ac3b..0c8cddcd73 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java +++ b/cps-service/src/main/java/org/onap/cps/spi/FetchDescendantsOption.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech * Copyright (C) 2022 Nordix Foundation + * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +21,11 @@ package org.onap.cps.spi; +import com.google.common.base.Strings; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import lombok.RequiredArgsConstructor; +import org.onap.cps.spi.exceptions.DataValidationException; @RequiredArgsConstructor public class FetchDescendantsOption { @@ -29,6 +34,9 @@ public class FetchDescendantsOption { public static final FetchDescendantsOption OMIT_DESCENDANTS = new FetchDescendantsOption(0); public static final FetchDescendantsOption INCLUDE_ALL_DESCENDANTS = new FetchDescendantsOption(-1); + private static final Pattern FETCH_DESCENDANTS_OPTION_PATTERN = + Pattern.compile("^$|^all$|^none$|^[0-9]+$|^-1$"); + private final int depth; /** @@ -58,6 +66,36 @@ public class FetchDescendantsOption { return nextDescendantsOption; } + /** + * get fetch descendants option for given descendant. + * + * @param fetchDescendantsOptionAsString fetch descendants option string + * @return fetch descendants option for given descendant + */ + public static FetchDescendantsOption getFetchDescendantsOption(final String fetchDescendantsOptionAsString) { + validateFetchDescendantsOption(fetchDescendantsOptionAsString); + if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString) + || "0".equals(fetchDescendantsOptionAsString) || "none".equals(fetchDescendantsOptionAsString)) { + return FetchDescendantsOption.OMIT_DESCENDANTS; + } else if ("-1".equals(fetchDescendantsOptionAsString) || "all".equals(fetchDescendantsOptionAsString)) { + return FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; + } else { + final Integer depth = Integer.valueOf(fetchDescendantsOptionAsString); + return new FetchDescendantsOption(depth); + } + } + + private static void validateFetchDescendantsOption(final String fetchDescendantsOptionAsString) { + if (Strings.isNullOrEmpty(fetchDescendantsOptionAsString)) { + return; + } + final Matcher matcher = FETCH_DESCENDANTS_OPTION_PATTERN.matcher(fetchDescendantsOptionAsString); + if (!matcher.matches()) { + throw new DataValidationException("FetchDescendantsOption validation error.", + fetchDescendantsOptionAsString + " is not valid fetch descendants option"); + } + } + private static void validateDepth(final int depth) { if (depth < -1) { throw new IllegalArgumentException("A depth of less than minus one is not allowed"); diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsQueryServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsQueryServiceImplSpec.groovy index 8b232b420d..60286b6643 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsQueryServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsQueryServiceImplSpec.groovy @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021-2022 Nordix Foundation + * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,7 +44,8 @@ class CpsQueryServiceImplSpec extends Specification { and: 'the CpsValidator is called on the dataspaceName, schemaSetName and anchorName' 1 * mockCpsValidator.validateNameCharacters(dataspaceName, anchorName) where: 'all fetch descendants options are supported' - fetchDescendantsOption << [FetchDescendantsOption.OMIT_DESCENDANTS, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS] + fetchDescendantsOption << [FetchDescendantsOption.OMIT_DESCENDANTS, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS, + FetchDescendantsOption.FETCH_DIRECT_CHILDREN_ONLY, new FetchDescendantsOption(10)] } } diff --git a/cps-service/src/test/groovy/org/onap/cps/spi/FetchDescendantsOptionSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/spi/FetchDescendantsOptionSpec.groovy index 627383561a..c4d3dd8b7b 100644 --- a/cps-service/src/test/groovy/org/onap/cps/spi/FetchDescendantsOptionSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/spi/FetchDescendantsOptionSpec.groovy @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2022 Nordix Foundation + * Modifications Copyright (C) 2023 TechMahindra Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +21,7 @@ package org.onap.cps.spi - +import org.onap.cps.spi.exceptions.DataValidationException import spock.lang.Specification class FetchDescendantsOptionSpec extends Specification { @@ -72,4 +73,18 @@ class FetchDescendantsOptionSpec extends Specification { then: 'exception thrown' thrown IllegalArgumentException } + + def 'Create fetch descendant option with descendant using #scenario'() { + when: 'the next level of depth is not allowed' + def FetchDescendantsOption fetchDescendantsOption = FetchDescendantsOption.getFetchDescendantsOption(fetchDescendantsOptionAsString) + then: 'fetch descendant object created' + assert fetchDescendantsOption.depth == expectedDepth + where: 'following parameters are used' + scenario | fetchDescendantsOptionAsString || expectedDepth + 'all descendants using number' | '-1' || -1 + 'all descendants using all' | 'all' || -1 + 'No descendants by default' | '' || 0 + 'No descendants using none' | 'none' || 0 + 'til 10th descendants using number' | '10' || 10 + } } |