diff options
Diffstat (limited to 'cps-ri/src/test')
4 files changed, 209 insertions, 78 deletions
diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy new file mode 100644 index 0000000000..6a0a6f48ba --- /dev/null +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceQueryDataNodeSpec.groovy @@ -0,0 +1,174 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2021 Pantheon.tech + * Modifications Copyright (C) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.cps.spi.impl + +import com.google.common.collect.ImmutableSet +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import org.onap.cps.spi.CpsDataPersistenceService +import org.onap.cps.spi.FetchDescendantsOption +import org.onap.cps.spi.exceptions.CpsPathException +import org.onap.cps.spi.model.DataNode +import org.onap.cps.spi.model.DataNodeBuilder +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.jdbc.Sql +import spock.lang.Unroll + +import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS +import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS + +class CpsDataPersistenceQueryDataNodeSpec extends CpsPersistenceSpecBase { + + @Autowired + CpsDataPersistenceService objectUnderTest + + static final Gson GSON = new GsonBuilder().create() + + static final String SET_DATA = '/data/fragment.sql' + static final String XPATH_DATA_NODE_WITH_DESCENDANTS = '/parent-1' + + static DataNode existingDataNode + static DataNode existingChildDataNode + + def expectedLeavesByXpathMap = [ + '/parent-100' : ['parent-leaf': 'parent-leaf value'], + '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf value'], + '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf value'], + '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value'] + ] + + static { + existingDataNode = createDataNodeTree(XPATH_DATA_NODE_WITH_DESCENDANTS) + existingChildDataNode = createDataNodeTree('/parent-1/child-1') + } + + static def createDataNodeTree(String... xpaths) { + def dataNodeBuilder = new DataNodeBuilder().withXpath(xpaths[0]) + if (xpaths.length > 1) { + def xPathsDescendant = Arrays.copyOfRange(xpaths, 1, xpaths.length) + def childDataNode = createDataNodeTree(xPathsDescendant) + dataNodeBuilder.withChildDataNodes(ImmutableSet.of(childDataNode)) + } + dataNodeBuilder.build() + } + + def static treeToFlatMapByXpath(Map<String, DataNode> flatMap, DataNode dataNodeTree) { + flatMap.put(dataNodeTree.getXpath(), dataNodeTree) + dataNodeTree.getChildDataNodes() + .forEach(childDataNode -> treeToFlatMapByXpath(flatMap, childDataNode)) + return flatMap + } + + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query for single leaf value with type: #type.'() { + when: 'a query is executed to get a data node by the given cps path' + def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption) + then: 'the correct data is returned' + def leaves = '[common-leaf-name:common-leaf value, common-leaf-name-int:5.0]' + DataNode dataNode = result.stream().findFirst().get() + dataNode.getLeaves().toString() == leaves + dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes + where: 'the following data is used' + type | cpsPath | includeDescendantsOption || expectedNumberOfChidlNodes + 'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf value\']' | OMIT_DESCENDANTS || 0 + 'Integer and descendants' | '/parent-200/child-202[@common-leaf-name-int=5]' | INCLUDE_ALL_DESCENDANTS || 1 + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() { + when: 'a query is executed to get datanodes for the given cps path' + def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS) + then: 'no data is returned' + result.isEmpty() + where: 'following cps queries are performed' + scenario | cpsPath + 'cps path is incomplete' | '/parent-200[@common-leaf-name-int=5]' + 'leaf value does not exist' | '/parent-200/child-202[@common-leaf-name=\'does not exist\']' + 'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]' + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query using descendant anywhere and #type (further) descendants.'() { + when: 'a query is executed to get a data node by the given cps path' + def cpsPath = '//child-202' + def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption) + then: 'the data node has the correct number of children' + DataNode dataNode = result.stream().findFirst().get() + dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes + where: 'the following data is used' + type | includeDescendantsOption || expectedNumberOfChildNodes + 'omit' | OMIT_DESCENDANTS || 0 + 'include' | INCLUDE_ALL_DESCENDANTS || 1 + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query using descendant anywhere with %scenario '() { + when: 'a query is executed to get a data node by the given cps path' + def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS) + then: 'the correct number of data nodes are retrieved' + result.size() == expectedXPaths.size() + and: 'xpaths of the retrieved data nodes are as expected' + for(int i = 0; i<result.size(); i++) { + result[i].getXpath() == expectedXPaths[i] + } + where: 'the following data is used' + scenario | cpsPath || expectedXPaths + 'fully unique descendant name' | '//grand-child-202' || ['/parent-200/child-202/grand-child-202'] + 'descendant name match end of other node' | '//child-202' || ['/parent-200/child-202','/parent-201/child-202'] + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query using descendant anywhere ends with yang list containing %scenario '() { + when: 'a query is executed to get a data node by the given cps path' + def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS) + then: 'the correct number of data nodes are retrieved' + result.size() == expectedXPaths.size() + and: 'xpaths of the retrieved data nodes are as expected' + for(int i = 0; i<result.size(); i++) { + result[i].getXpath() == expectedXPaths[i] + } + where: 'the following data is used' + scenario | cpsPath || expectedXPaths + 'one attribute' | '//child-202[@common-leaf-name-int=5]' || ['/parent-200/child-202','/parent-201/child-202'] + 'trailing "and" is ignored' | '//child-202[@common-leaf-name-int=5 and]' || ['/parent-200/child-202','/parent-201/child-202'] + 'more than one attribute' | '//child-202[@common-leaf-name-int=5 and @common-leaf-name="common-leaf value"]' || ['/parent-200/child-202'] + 'attributes reversed in order' | '//child-202[@common-leaf-name="common-leaf value" and @common-leaf-name-int=5]' || ['/parent-200/child-202'] + } + + @Unroll + @Sql([CLEAR_DATA, SET_DATA]) + def 'Cps Path query error scenario using descendant anywhere ends with yang list containing %scenario '() { + when: 'a query is executed to get a data node by the given cps path' + def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS) + then: 'exception is thrown' + thrown(CpsPathException) + where: 'the following data is used' + scenario | cpsPath + 'one of the attributes without value' | '//child-202[@common-leaf-name-int=5 and @another-attribute"]' + 'more than one attribute separated by or' | '//child-202[@common-leaf-name-int=5 or @common-leaf-name="common-leaf value"]' + } +} diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy index 24aa3d41fa..a47bd65d02 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/impl/CpsDataPersistenceServiceSpec.groovy @@ -24,7 +24,6 @@ import com.google.common.collect.ImmutableSet import com.google.gson.Gson import com.google.gson.GsonBuilder import org.onap.cps.spi.CpsDataPersistenceService -import org.onap.cps.spi.FetchDescendantsOption import org.onap.cps.spi.entities.FragmentEntity import org.onap.cps.spi.exceptions.AlreadyDefinedException import org.onap.cps.spi.exceptions.AnchorNotFoundException @@ -61,10 +60,10 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase { static DataNode existingChildDataNode def expectedLeavesByXpathMap = [ - '/parent-100' : ['parent-leaf': 'parent-leaf-value'], - '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf-value'], - '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf-value'], - '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf-value'] + '/parent-100' : ['parent-leaf': 'parent-leaf value'], + '/parent-100/child-001' : ['first-child-leaf': 'first-child-leaf value'], + '/parent-100/child-002' : ['second-child-leaf': 'second-child-leaf value'], + '/parent-100/child-002/grand-child': ['grand-child-leaf': 'grand-child-leaf value'] ] static { @@ -326,65 +325,4 @@ class CpsDataPersistenceServiceSpec extends CpsPersistenceSpecBase { static Map<String, Object> getLeavesMap(FragmentEntity fragmentEntity) { return GSON.fromJson(fragmentEntity.getAttributes(), Map<String, Object>.class) } - - @Unroll - @Sql([CLEAR_DATA, SET_DATA]) - def 'Cps Path query for single leaf value with type: #type.'() { - when: 'a query is executed to get a data node by the given cps path' - def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption) - then: 'the correct data is returned' - def leaves = '[common-leaf-name:common-leaf-value, common-leaf-name-int:5.0]' - DataNode dataNode = result.stream().findFirst().get() - dataNode.getLeaves().toString() == leaves - dataNode.getChildDataNodes().size() == expectedNumberOfChidlNodes - where: 'the following data is used' - type | cpsPath | includeDescendantsOption || expectedNumberOfChidlNodes - 'String and no descendants' | '/parent-200/child-202[@common-leaf-name=\'common-leaf-value\']' | OMIT_DESCENDANTS || 0 - 'Integer and descendants' | '/parent-200/child-202[@common-leaf-name-int=5]' | INCLUDE_ALL_DESCENDANTS || 1 - } - - @Unroll - @Sql([CLEAR_DATA, SET_DATA]) - def 'Query for attribute by cps path with cps paths that return no data because of #scenario.'() { - when: 'a query is executed to get datanodes for the given cps path' - def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS) - then: 'no data is returned' - result.isEmpty() - where: 'following cps queries are performed' - scenario | cpsPath - 'cps path is incomplete' | '/parent-200[@common-leaf-name-int=5]' - 'leaf value does not exist' | '/parent-200/child-202[@common-leaf-name=\'does not exist\']' - 'incomplete end of xpath prefix' | '/parent-200/child-20[@common-leaf-name-int=5]' - } - - @Unroll - @Sql([CLEAR_DATA, SET_DATA]) - def 'Cps Path query using descendant anywhere and #type (further) descendants.'() { - when: 'a query is executed to get a data node by the given cps path' - def cpsPath = '//child-202' - def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, includeDescendantsOption) - then: 'the data node has the correct number of children' - DataNode dataNode = result.stream().findFirst().get() - dataNode.getChildDataNodes().size() == expectedNumberOfChildNodes - where: 'the following data is used' - type | includeDescendantsOption || expectedNumberOfChildNodes - 'omit' | OMIT_DESCENDANTS || 0 - 'include' | INCLUDE_ALL_DESCENDANTS || 1 - } - - @Unroll - @Sql([CLEAR_DATA, SET_DATA]) - def 'Cps Path query using descendant anywhere with %scenario '() { - when: 'a query is executed to get a data node by the given cps path' - def result = objectUnderTest.queryDataNodes(DATASPACE_NAME, ANCHOR_FOR_DATA_NODES_WITH_LEAVES, cpsPath, OMIT_DESCENDANTS) - then: 'Only one data node is returned' - result.size() == 1 - and: - result.stream().findFirst().get().xpath == expectedXPath - where: 'the following data is used' - scenario | cpsPath || expectedXPath - 'fully unique descendant name' | '//grand-child-202' || '/parent-200/child-202/grand-child-202' - 'descendant name and parent' | '//child-202/grand-child-202' || '/parent-200/child-202/grand-child-202' - 'descendant name match end of other node' | '//child-202' || '/parent-200/child-202' - } } diff --git a/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy b/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy index 7f558dd10b..99302a4013 100644 --- a/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy +++ b/cps-ri/src/test/groovy/org/onap/cps/spi/query/CpsPathQuerySpec.groovy @@ -1,6 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2020-2021 Bell Canada. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -56,21 +57,37 @@ class CpsPathQuerySpec extends Specification { where: 'the following data is used' scenario | cpsPath || expectedEndsWithValue 'yang container' | '//cps-path' || 'cps-path' - 'yang list' | '//cps-path[@key=value]' || 'cps-path[@key=value]' 'parent & child' | '//parent/child' || 'parent/child' } @Unroll + def 'Parse cps path that ends with a yang list containing #scenario.'() { + when: 'the given cps path is parsed' + def result = objectUnderTest.createFrom(cpsPath) + then: 'the query has the right type' + result.cpsPathQueryType == CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES + and: 'the right ends with parameters are set' + result.descendantName == "child" + result.leavesData.size() == expectedNumberOfLeaves + where: 'the following data is used' + scenario | cpsPath || expectedNumberOfLeaves + 'one attribute' | '//child[@common-leaf-name-int=5]' || 1 + 'more than one attribute' | '//child[@int-leaf=5 and @leaf-name="leaf value"]' || 2 + } + + @Unroll def 'Parse cps path with #scenario.'() { when: 'the given cps path is parsed' objectUnderTest.createFrom(cpsPath) then: 'a CpsPathException is thrown' thrown(CpsPathException) where: 'the following data is used' - scenario | cpsPath - 'no / at the start' | 'invalid-cps-path/child' - 'additional / after descendant option' | '///cps-path' - 'float value' | '/parent-200/child-202[@common-leaf-name-float=5.0]' - 'too many containers' | '/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100[@a=1]' + scenario | cpsPath + 'no / at the start' | 'invalid-cps-path/child' + 'additional / after descendant option' | '///cps-path' + 'float value' | '/parent-200/child-202[@common-leaf-name-float=5.0]' + 'too many containers' | '/1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43/44/45/46/47/48/49/50/51/52/53/54/55/56/57/58/59/60/61/62/63/64/65/66/67/68/69/70/71/72/73/74/75/76/77/78/79/80/81/82/83/84/85/86/87/88/89/90/91/92/93/94/95/96/97/98/99/100[@a=1]' + 'end with descendant and more than one attribute separated by "or"' | '//child[@int-leaf=5 or @leaf-name="leaf value"]' + 'missing attribute value' | '//child[@int-leaf=5 and @name]' } } diff --git a/cps-ri/src/test/resources/data/fragment.sql b/cps-ri/src/test/resources/data/fragment.sql index 3c1f793b93..b6dc2ca307 100644 --- a/cps-ri/src/test/resources/data/fragment.sql +++ b/cps-ri/src/test/resources/data/fragment.sql @@ -17,14 +17,16 @@ INSERT INTO FRAGMENT (ID, XPATH, ANCHOR_ID, PARENT_ID, DATASPACE_ID) VALUES (4006, '/parent-1/child-1/grandchild-1', 3001, 4004, 1001); INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) VALUES - (4101, 1001, 3003, null, '/parent-100', '{"parent-leaf": "parent-leaf-value"}'), - (4102, 1001, 3003, 4101, '/parent-100/child-001', '{"first-child-leaf": "first-child-leaf-value"}'), - (4103, 1001, 3003, 4101, '/parent-100/child-002', '{"second-child-leaf": "second-child-leaf-value"}'), - (4104, 1001, 3003, 4103, '/parent-100/child-002/grand-child', '{"grand-child-leaf": "grand-child-leaf-value"}'); + (4101, 1001, 3003, null, '/parent-100', '{"parent-leaf": "parent-leaf value"}'), + (4102, 1001, 3003, 4101, '/parent-100/child-001', '{"first-child-leaf": "first-child-leaf value"}'), + (4103, 1001, 3003, 4101, '/parent-100/child-002', '{"second-child-leaf": "second-child-leaf value"}'), + (4104, 1001, 3003, 4103, '/parent-100/child-002/grand-child', '{"grand-child-leaf": "grand-child-leaf value"}'); INSERT INTO FRAGMENT (ID, DATASPACE_ID, ANCHOR_ID, PARENT_ID, XPATH, ATTRIBUTES) VALUES (4201, 1001, 3003, null, '/parent-200', '{"leaf-value": "original"}'), (4202, 1001, 3003, 4201, '/parent-200/child-201', '{"leaf-value": "original"}'), (4203, 1001, 3003, 4202, '/parent-200/child-201/grand-child', '{"leaf-value": "original"}'), - (4204, 1001, 3003, 4201, '/parent-200/child-202', '{"common-leaf-name": "common-leaf-value", "common-leaf-name-int" : 5}'), - (4205, 1001, 3003, 4204, '/parent-200/child-202/grand-child-202', '{"common-leaf-name": "common-leaf-value", "common-leaf-name-int" : 5}');
\ No newline at end of file + (4204, 1001, 3003, 4201, '/parent-200/child-202', '{"common-leaf-name": "common-leaf value", "common-leaf-name-int" : 5}'), + (4205, 1001, 3003, 4204, '/parent-200/child-202/grand-child-202', '{"common-leaf-name": "common-leaf value", "common-leaf-name-int" : 5}'), + (4206, 1001, 3003, null, '/parent-201', '{"leaf-value": "original"}'), + (4207, 1001, 3003, 4201, '/parent-201/child-202', '{"common-leaf-name": "common-leaf other value", "common-leaf-name-int" : 5}');
\ No newline at end of file |