65a0d54f4035ae010877440645f0bff122b25da0
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / CpsDataServiceImplSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.api.impl
21
22 import org.onap.cps.TestUtils
23 import org.onap.cps.api.CpsAdminService
24 import org.onap.cps.api.CpsModuleService
25 import org.onap.cps.spi.CpsDataPersistenceService
26 import org.onap.cps.spi.FetchDescendantsOption
27 import org.onap.cps.spi.model.Anchor
28 import org.onap.cps.spi.model.DataNodeBuilder
29 import org.onap.cps.yang.YangTextSchemaSourceSet
30 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
31 import spock.lang.Specification
32 import spock.lang.Unroll
33
34 class CpsDataServiceImplSpec extends Specification {
35     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
36     def mockCpsAdminService = Mock(CpsAdminService)
37     def mockCpsModuleService = Mock(CpsModuleService)
38     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
39
40     def objectUnderTest = new CpsDataServiceImpl()
41
42     def setup() {
43         objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService
44         objectUnderTest.cpsAdminService = mockCpsAdminService
45         objectUnderTest.cpsModuleService = mockCpsModuleService
46         objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache
47     }
48
49     def dataspaceName = 'some dataspace'
50     def anchorName = 'some anchor'
51     def schemaSetName = 'some schema set'
52
53     def 'Saving json data.'() {
54         given: 'that the admin service will return an anchor'
55             def anchor = new Anchor()
56             anchor.name = anchorName
57             anchor.schemaSetName = schemaSetName
58             mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
59         and: 'the schema source set cache returns a schema source set'
60             def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
61             mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
62         and: 'the schema source sets returns the test-tree schema context'
63             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
64             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
65             mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext
66         when: 'save data method is invoked with test-tree json data'
67             def jsonData = TestUtils.getResourceFileContent('test-tree.json')
68             objectUnderTest.saveData(dataspaceName, anchorName, jsonData)
69         then: 'the persistence service method is invoked with correct parameters'
70             1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
71                     { dataNode -> dataNode.xpath == '/test-tree' })
72     }
73
74     @Unroll
75     def 'Get data node with option #fetchChildrenOption'() {
76         def xpath = '/xpath'
77         def dataNode = new DataNodeBuilder().withXpath(xpath).build()
78         given: 'persistence service returns data for get data request'
79             mockCpsDataPersistenceService.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) >> dataNode
80         expect: 'service returns same data if uses same parameters'
81             objectUnderTest.getDataNode(dataspaceName, anchorName, xpath, fetchDescendantsOption) == dataNode
82         where: 'all fetch options are supported'
83             fetchDescendantsOption << FetchDescendantsOption.values()
84     }
85 }