Upgrade to Java 17
[cps.git] / cps-service / src / test / groovy / org / onap / cps / utils / PrefixResolverSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 Bell Canada.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.utils
24
25 import com.hazelcast.map.IMap
26 import org.onap.cps.TestUtils
27 import org.onap.cps.api.CpsAdminService
28 import org.onap.cps.api.impl.YangTextSchemaSourceSetCache
29 import org.onap.cps.cache.AnchorDataCacheEntry
30 import org.onap.cps.spi.model.Anchor
31 import org.onap.cps.yang.YangTextSchemaSourceSet
32 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
33 import spock.lang.Specification
34
35 class PrefixResolverSpec extends Specification {
36
37     def mockCpsAdminService = Mock(CpsAdminService)
38
39     def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache)
40
41     def mockAnchorDataCache = Mock(IMap<String, AnchorDataCacheEntry>)
42
43     def objectUnderTest = new PrefixResolver(mockCpsAdminService, mockYangTextSchemaSourceSetCache, mockAnchorDataCache)
44
45     def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
46
47     def yangResourceNameToContent = TestUtils.getYangResourcesAsMap('test-tree.yang')
48
49     def schemaContext = YangTextSchemaSourceSetBuilder.of(yangResourceNameToContent).getSchemaContext()
50
51     def setup() {
52         given: 'an anchor for the test-tree model'
53             def anchor = new Anchor(dataspaceName: 'testDataspace', name: 'testAnchor')
54         and: 'the system can get this anchor'
55             mockCpsAdminService.getAnchor('testDataspace', 'testAnchor') >> anchor
56         and: 'the schema source cache contains the schema context for the test-tree module'
57             mockYangTextSchemaSourceSet.getSchemaContext() >> schemaContext
58     }
59
60     def 'get xpath prefix using node schema context'() {
61         when: 'the prefix of the yang module is retrieved'
62             def result = objectUnderTest.getPrefix('testDataspace', 'testAnchor', xpath)
63         then: 'the expected prefix is returned'
64             result == expectedPrefix
65         and: 'the cache is updated for the given anchor with a map of prefixes per top level container (just one one this case)'
66             1 * mockAnchorDataCache.put('testAnchor',_ , _ ,_) >> { args -> {
67                 def prefixPerContainerName = args[1].getProperty("prefixPerContainerName")
68                 assert prefixPerContainerName.size() == 1
69                 assert prefixPerContainerName.get('test-tree') == 'tree'
70                 }
71             }
72         and: 'schema source cache is used (i.e. need to build schema context)'
73             1 * mockYangTextSchemaSourceSetCache.get(*_) >> mockYangTextSchemaSourceSet
74         where: 'the following scenarios are applied'
75             xpath                         || expectedPrefix
76             '/test-tree'                  || 'tree'
77             '/test-tree/with/descendants' || 'tree'
78             '/test-tree[@id=1]'           || 'tree'
79             '/test-tree[@id=1]/child'     || 'tree'
80             '/not-defined'                || ''
81             'invalid-xpath'               || ''
82     }
83
84     def 'get prefix with populated anchor data cache with #scenario cache entry'() {
85         given: 'anchor data cache is populated for the anchor with a prefix for top level container named #cachedTopLevelContainerName'
86             def anchorDataCacheEntry = new AnchorDataCacheEntry()
87             def prefixPerContainerName = [(cachedTopLevelContainerName): 'cachedPrefix']
88             anchorDataCacheEntry.setProperty('prefixPerContainerName',prefixPerContainerName)
89             mockAnchorDataCache.containsKey('testAnchor') >> true
90             mockAnchorDataCache.get('testAnchor') >> anchorDataCacheEntry
91         when: 'the prefix of the yang module is retrieved'
92             def result = objectUnderTest.getPrefix('testDataspace', 'testAnchor', '/test-tree')
93         then: 'the expected prefix is returned'
94             result == expectedPrefix
95         and: 'schema source cache is not used (i.e. no need to build schema context)'
96             0 * mockYangTextSchemaSourceSetCache.get(*_)
97         where: 'the following scenarios are applied'
98             scenario       | cachedTopLevelContainerName || expectedPrefix
99             'matching'     | 'test-tree'                 || 'cachedPrefix'
100             'non-matching' | 'other'                     || ''
101     }
102
103     def 'get prefix with other (non relevant) data in anchor data cache'() {
104         given: 'anchor data cache is populated with non relevant other property'
105             def anchorDataCacheEntry = new AnchorDataCacheEntry()
106             anchorDataCacheEntry.setProperty('something else', 'does not matter')
107             mockAnchorDataCache.containsKey('testAnchor') >> true
108             mockAnchorDataCache.get('testAnchor') >> anchorDataCacheEntry
109         when: 'the prefix of the yang module is retrieved'
110             def result = objectUnderTest.getPrefix('testDataspace', 'testAnchor', '/test-tree')
111         then: 'the expected prefix is returned'
112             result == 'tree'
113         and: 'schema source cache is used (i.e. need to build schema context)'
114             1 * mockYangTextSchemaSourceSetCache.get(*_) >> mockYangTextSchemaSourceSet
115     }
116
117 }