From: niamhcore Date: Thu, 17 Jun 2021 14:27:55 +0000 (+0100) Subject: Updating exception and explanation for update node leaves X-Git-Tag: 1.1.0~34^2 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=657a97124f0890f6bb5fec61a37978a6aa00a29d;p=cps.git Updating exception and explanation for update node leaves Issue-ID: CPS-464 & CPS-465 Signed-off-by: niamhcore Change-Id: I1880ceee66d89c49d039011163fa74bb7da19f25 --- diff --git a/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java b/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java index 762e61ac5..4a9957deb 100644 --- a/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java +++ b/cps-service/src/main/java/org/onap/cps/spi/model/DataNodeBuilder.java @@ -29,6 +29,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; +import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.utils.YangUtils; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; @@ -142,6 +143,10 @@ public class DataNodeBuilder { private DataNode buildFromNormalizedNodeTree() { final Collection dataNodeCollection = buildCollectionFromNormalizedNodeTree(); + if (!dataNodeCollection.iterator().hasNext()) { + throw new DataValidationException( + "Unsupported xpath: ", "Unsupported xpath as it is referring to one element"); + } return dataNodeCollection.iterator().next(); } diff --git a/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java index 0b66d37fb..b5c14a072 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java +++ b/cps-service/src/main/java/org/onap/cps/utils/YangUtils.java @@ -94,9 +94,13 @@ public class YangUtils { final var jsonReader = new JsonReader(new StringReader(jsonData)); jsonParserStream.parse(jsonReader); - } catch (final IOException | IllegalStateException | JsonSyntaxException exception) { + } catch (final IOException | JsonSyntaxException exception) { throw new DataValidationException( - "Failed to parse json data: " + jsonData, exception.getMessage(), exception); + "Failed to parse json data: " + jsonData, exception.getMessage(), exception); + } catch (final IllegalStateException illegalStateException) { + throw new DataValidationException( + "Failed to parse json data. Unsupported xpath or json data:" + jsonData, illegalStateException + .getMessage(), illegalStateException); } return normalizedNodeResult.getResult(); } diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy index 27a5a4e0c..8001d6a9f 100644 --- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy @@ -125,6 +125,19 @@ class CpsDataServiceImplSpec extends Specification { 'level 2 node' | '/test-tree' | '{"branch": [{"name":"Name"}]}' || '/test-tree/branch[@name=\'Name\']' | ['name': 'Name'] } + def 'Update list-element data node with : #scenario.'() { + given: 'schema set for given anchor and dataspace references bookstore model' + setupSchemaSetMocks('bookstore.yang') + when: 'update data method is invoked with json data #jsonData and parent node xpath' + objectUnderTest.updateNodeLeaves(dataspaceName, anchorName, '/bookstore/categories[@code=2]', jsonData) + then: 'the persistence service method is invoked with correct parameters' + thrown(DataValidationException) + where: 'following parameters were used' + scenario | jsonData + 'multiple leaves' | '{"code": "01","name": "some-name"}' + 'one leaf' | '{"name": "some-name"}' + } + def 'Replace data node: #scenario.'() { given: 'schema set for given anchor and dataspace references test-tree model' setupSchemaSetMocks('test-tree.yang')