Refactor Delete Anchor functionality
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / E2ENetworkSliceSpec.groovy
1 /*\r
2  * ============LICENSE_START=======================================================\r
3  * Copyright (C) 2021 Nordix Foundation.\r
4  * Modifications Copyright (C) 2021-2022 Bell Canada.\r
5  * Modifications Copyright (C) 2021 Pantheon.tech\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  * SPDX-License-Identifier: Apache-2.0\r
20  * ============LICENSE_END=========================================================\r
21  */\r
22 \r
23 package org.onap.cps.api.impl\r
24 \r
25 import org.onap.cps.TestUtils\r
26 import org.onap.cps.api.CpsAdminService\r
27 import org.onap.cps.notification.NotificationService\r
28 import org.onap.cps.spi.CpsDataPersistenceService\r
29 import org.onap.cps.spi.CpsModulePersistenceService\r
30 import org.onap.cps.spi.model.Anchor\r
31 import org.onap.cps.utils.YangUtils\r
32 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder\r
33 import spock.lang.Specification\r
34 \r
35 class E2ENetworkSliceSpec extends Specification {\r
36     def mockModuleStoreService = Mock(CpsModulePersistenceService)\r
37     def mockDataStoreService = Mock(CpsDataPersistenceService)\r
38     def mockCpsAdminService = Mock(CpsAdminService)\r
39     def mockNotificationService = Mock(NotificationService)\r
40     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)\r
41     def cpsModuleServiceImpl = new CpsModuleServiceImpl(mockModuleStoreService,\r
42             mockYangTextSchemaSourceSetCache,mockCpsAdminService )\r
43     def cpsDataServiceImpl = new CpsDataServiceImpl(mockDataStoreService, mockCpsAdminService,\r
44             mockYangTextSchemaSourceSetCache, mockNotificationService)\r
45 \r
46     def dataspaceName = 'someDataspace'\r
47     def anchorName = 'someAnchor'\r
48     def schemaSetName = 'someSchemaSet'\r
49     def noTimestamp = null\r
50 \r
51     def 'E2E model can be parsed by CPS.'() {\r
52         given: 'Valid yang resource as name-to-content map'\r
53             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap(\r
54                     'ietf/ietf-inet-types@2013-07-15.yang',\r
55                     'ietf/ietf-yang-types@2013-07-15.yang',\r
56                     'e2e/basic/ran-network2020-08-06.yang'\r
57             )\r
58         when: 'Create schema set method is invoked'\r
59             cpsModuleServiceImpl.createSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap)\r
60         then: 'Parameters are validated and processing is delegated to persistence service'\r
61             1 * mockModuleStoreService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap)\r
62     }\r
63 \r
64     def 'E2E Coverage Area-Tracking Area & TA-Cell mapping model can be parsed by CPS.'() {\r
65         given: 'Valid yang resource as name-to-content map'\r
66             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap(\r
67                     'e2e/basic/cps-cavsta-onap-internal2021-01-28.yang')\r
68         when: 'Create schema set method is invoked'\r
69             cpsModuleServiceImpl.createSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap)\r
70         then: 'Parameters are validated and processing is delegated to persistence service'\r
71             1 * mockModuleStoreService.storeSchemaSet(dataspaceName, schemaSetName, yangResourcesNameToContentMap)\r
72     }\r
73 \r
74     def 'E2E Coverage Area-Tracking Area & TA-Cell mapping data can be parsed by CPS.'() {\r
75         given: 'Valid yang resource as name-to-content map'\r
76             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap(\r
77                     'e2e/basic/cps-cavsta-onap-internal2021-01-28.yang')\r
78             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap).getSchemaContext()\r
79             def dataNodeStored\r
80         and : 'a valid json is provided for the model'\r
81             def jsonData = TestUtils.getResourceFileContent('e2e/basic/cps-Cavsta-Data.txt')\r
82         and : 'all the further dependencies are mocked '\r
83             mockCpsAdminService.getAnchor(dataspaceName, anchorName) >>\r
84                     new Anchor().builder().name(anchorName).schemaSetName(schemaSetName).build()\r
85             mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >>\r
86                     YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)\r
87             mockModuleStoreService.getYangSchemaResources(dataspaceName, schemaSetName) >> schemaContext\r
88         when: 'saveData method is invoked'\r
89             cpsDataServiceImpl.saveData(dataspaceName, anchorName, jsonData, noTimestamp)\r
90         then: 'Parameters are validated and processing is delegated to persistence service'\r
91             1 * mockDataStoreService.storeDataNode('someDataspace', 'someAnchor', _) >>\r
92                     { args -> dataNodeStored = args[2]}\r
93             def child = dataNodeStored.childDataNodes[0]\r
94             assert child.childDataNodes.size() == 1\r
95         and: 'list of Tracking Area for a Coverage Area are stored with correct xpath and child nodes '\r
96             def listOfTAForCoverageArea = child.childDataNodes[0]\r
97             listOfTAForCoverageArea.xpath == '/ran-coverage-area/pLMNIdList[@mcc=\'310\' and @mnc=\'410\']/' +\r
98                     'coverage-area[@coverageArea=\'Washington\']'\r
99             listOfTAForCoverageArea.childDataNodes[0].leaves.get('nRTAC') == 234\r
100         and: 'list of cells in a tracking area are stored with correct xpath and child nodes '\r
101             def listOfCellsInTrackingArea = listOfTAForCoverageArea.childDataNodes[0]\r
102             listOfCellsInTrackingArea.xpath == '/ran-coverage-area/pLMNIdList[@mcc=\'310\' and @mnc=\'410\']/' +\r
103                     'coverage-area[@coverageArea=\'Washington\']/coverageAreaTAList[@nRTAC=\'234\']'\r
104             listOfCellsInTrackingArea.childDataNodes[0].leaves.get('cellLocalId') == 15709\r
105     }\r
106 \r
107     def 'E2E Coverage Area-Tracking Area & TA-Cell mapping data can be parsed for RAN inventory.'() {\r
108         def dataNodeStored\r
109         given: 'valid yang resource as name-to-content map'\r
110             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap(\r
111                     'e2e/basic/cps-ran-inventory@2021-01-28.yang')\r
112             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap).getSchemaContext()\r
113         and : 'a valid json is provided for the model'\r
114             def jsonData = TestUtils.getResourceFileContent('e2e/basic/cps-ran-inventory-data.json')\r
115         and : 'all the further dependencies are mocked '\r
116             mockCpsAdminService.getAnchor('someDataspace', 'someAnchor') >>\r
117                     new Anchor().builder().name('someAnchor').schemaSetName('someSchemaSet').build()\r
118             mockYangTextSchemaSourceSetCache.get('someDataspace', 'someSchemaSet') >> YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)\r
119             mockModuleStoreService.getYangSchemaResources('someDataspace', 'someSchemaSet') >> schemaContext\r
120         when: 'saveData method is invoked'\r
121             cpsDataServiceImpl.saveData('someDataspace', 'someAnchor', jsonData, noTimestamp)\r
122         then: 'parameters are validated and processing is delegated to persistence service'\r
123             1 * mockDataStoreService.storeDataNode('someDataspace', 'someAnchor', _) >>\r
124                     { args -> dataNodeStored = args[2]}\r
125         and: 'the size of the tree is correct'\r
126             def cpsRanInventory = TestUtils.getFlattenMapByXpath(dataNodeStored)\r
127             assert  cpsRanInventory.size() == 4\r
128         and: 'ran-inventory contains the correct child node'\r
129             def ranInventory = cpsRanInventory.get('/ran-inventory')\r
130             def ranSlices = cpsRanInventory.get('/ran-inventory/ran-slices[@rannfnssiid=\'14559ead-f4fe-4c1c-a94c-8015fad3ea35\']')\r
131             def sliceProfilesList = cpsRanInventory.get('/ran-inventory/ran-slices[@rannfnssiid=\'14559ead-f4fe-4c1c-a94c-8015fad3ea35\']/sliceProfilesList[@sliceProfileId=\'f33a9dd8-ae51-4acf-8073-c9390c25f6f1\']')\r
132             def pLMNIdList = cpsRanInventory.get('/ran-inventory/ran-slices[@rannfnssiid=\'14559ead-f4fe-4c1c-a94c-8015fad3ea35\']/sliceProfilesList[@sliceProfileId=\'f33a9dd8-ae51-4acf-8073-c9390c25f6f1\']/pLMNIdList[@mcc=\'310\' and @mnc=\'410\']')\r
133             ranInventory.getChildDataNodes().size() == 1\r
134             ranInventory.getChildDataNodes().find( {it.xpath == ranSlices.xpath})\r
135         and: 'ranSlices contains the correct child node'\r
136             ranSlices.getChildDataNodes().size() == 1\r
137             ranSlices.getChildDataNodes().find( {it.xpath == sliceProfilesList.xpath})\r
138         and: 'sliceProfilesList contains the correct child node'\r
139             sliceProfilesList.getChildDataNodes().size() == 1\r
140             sliceProfilesList.getChildDataNodes().find( {it.xpath == pLMNIdList.xpath})\r
141         and: 'pLMNIdList contains no children'\r
142             pLMNIdList.getChildDataNodes().size() == 0\r
143 \r
144     }\r
145 \r
146     def 'E2E RAN Schema Model.'(){\r
147         given: 'yang resources'\r
148             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap(\r
149                     'ietf/ietf-inet-types@2013-07-15.yang',\r
150                     'ietf/ietf-yang-types@2013-07-15.yang',\r
151                     'e2e/basic/cps-ran-schema-model@2021-05-19.yang'\r
152             )\r
153         and : 'json data'\r
154             def jsonData = TestUtils.getResourceFileContent('e2e/basic/cps-ran-schema-model-data-v4.json')\r
155         expect: 'schema context is built with no exception indicating the schema set being valid '\r
156             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap).getSchemaContext()\r
157         and: 'data is parsed with no exception indicating the model match'\r
158             YangUtils.parseJsonData(jsonData, schemaContext) != null\r
159     }\r
160 }\r