Merge "Decouple configuration from application"
[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.model.Anchor
27 import org.onap.cps.yang.YangTextSchemaSourceSet
28 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
29 import spock.lang.Specification
30
31 class CpsDataServiceImplSpec extends Specification {
32     def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService)
33     def mockCpsAdminService = Mock(CpsAdminService)
34     def mockCpsModuleService = Mock(CpsModuleService)
35     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
36
37     def objectUnderTest = new CpsDataServiceImpl()
38
39     def setup() {
40         objectUnderTest.cpsDataPersistenceService = mockCpsDataPersistenceService;
41         objectUnderTest.cpsAdminService = mockCpsAdminService;
42         objectUnderTest.cpsModuleService = mockCpsModuleService;
43         objectUnderTest.yangTextSchemaSourceSetCache = mockYangTextSchemaSourceSetCache;
44     }
45
46     def dataspaceName = 'some dataspace'
47     def anchorName = 'some anchor'
48     def schemaSetName = 'some schema set'
49
50     def 'Saving json data.'() {
51         given: 'that the admin service will return an anchor'
52             def anchor = new Anchor()
53             anchor.name = anchorName
54             anchor.schemaSetName = schemaSetName
55             mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
56         and: 'the schema source set cache returns a schema source set'
57             def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
58             mockYangTextSchemaSourceSetCache.get(dataspaceName,schemaSetName) >> mockYangTextSchemaSourceSet
59         and: 'the schema source sets returns the test-tree schema context'
60             def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
61             def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
62             mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext
63         when: 'save data method is invoked with test-tree json data'
64             def jsonData = org.onap.cps.TestUtils.getResourceFileContent('test-tree.json')
65             objectUnderTest.saveData(dataspaceName, anchorName, jsonData)
66         then: 'the persistence service method is invoked with correct parameters'
67             1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
68                     { dataNode -> dataNode.xpath == '/test-tree' })
69     }
70 }