Merge "CM SUBSCRIPTION: add new subscription for non existing xpath"
[cps.git] / cps-service / src / test / groovy / org / onap / cps / api / impl / YangTextSchemaSourceSetCacheSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Bell Canada
4  *  Modifications Copyright (C) 2022 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.api.impl
23
24 import org.onap.cps.TestUtils
25 import org.onap.cps.spi.CpsModulePersistenceService
26 import org.onap.cps.yang.YangTextSchemaSourceSet
27 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.test.context.SpringBootTest
31 import org.springframework.cache.Cache
32 import org.springframework.cache.CacheManager
33 import org.springframework.cache.annotation.EnableCaching
34 import org.springframework.cache.caffeine.CaffeineCacheManager
35 import org.springframework.test.context.ContextConfiguration
36 import spock.lang.Specification
37 import org.onap.cps.spi.utils.CpsValidator
38
39
40 @SpringBootTest
41 @EnableCaching
42 @ContextConfiguration(classes = [YangTextSchemaSourceSetCache, CaffeineCacheManager])
43 class YangTextSchemaSourceSetCacheSpec extends Specification {
44
45     @SpringBean
46     CpsModulePersistenceService mockModuleStoreService = Mock()
47
48     @SpringBean
49     CpsValidator mockCpsValidator = Mock(CpsValidator)
50
51     @Autowired
52     YangTextSchemaSourceSetCache objectUnderTest
53
54     @Autowired
55     CacheManager cacheManager
56
57     Cache yangResourceCacheImpl;
58
59     def setup() {
60         yangResourceCacheImpl = cacheManager.getCache('yangSchema')
61         yangResourceCacheImpl.clear()
62     }
63
64
65     def 'Cache Miss: Fetch data from Module persistence'() {
66         given: 'cache is empty'
67             yangResourceCacheImpl.clear()
68         and: 'a schema set exists'
69             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
70             def expectedYangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
71         when: 'schema-set information is asked'
72             def result = objectUnderTest.get('my-dataspace', 'my-schemaset')
73         then: 'information fetched from cps module persistence'
74             1 * mockModuleStoreService.getYangSchemaResources('my-dataspace', 'my-schemaset')
75                     >> yangResourcesNameToContentMap
76         and: 'stored in the cache'
77             def cachedValue = getCachedValue('my-dataspace', 'my-schemaset')
78             assert cachedValue.getModuleReferences() == expectedYangTextSchemaSourceSet.getModuleReferences()
79         and: 'the response is as expected'
80             assert result.getModuleReferences() == expectedYangTextSchemaSourceSet.getModuleReferences()
81         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
82             1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
83     }
84
85     def 'Cache Hit: Respond from cache'() {
86         given: 'a schema set exists'
87             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
88             def expectedYangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
89         and: 'stored in cache'
90             yangResourceCacheImpl.put(getCacheKey('my-dataspace', 'my-schemaset'), expectedYangTextSchemaSourceSet)
91         when: 'schema-set information is asked'
92             def result = objectUnderTest.get('my-dataspace', 'my-schemaset')
93         then: 'expected value is returned'
94             result.getModuleReferences() == expectedYangTextSchemaSourceSet.getModuleReferences()
95         and: 'module persistence is not invoked'
96             0 * mockModuleStoreService.getYangSchemaResources(_, _)
97     }
98
99     def 'Cache Update: when no data exist in the cache'() {
100         given: 'a schema set exists'
101             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
102             def yangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
103         when: 'cache is updated'
104             objectUnderTest.updateCache('my-dataspace', 'my-schemaset', yangTextSchemaSourceSet)
105         then: 'cached value is same as expected'
106             def cachedValue = getCachedValue('my-dataspace', 'my-schemaset')
107             cachedValue.getModuleReferences() == yangTextSchemaSourceSet.getModuleReferences()
108         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
109             1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
110     }
111
112     def 'Cache Evict:with invalid #scenario'() {
113         given: 'a schema set exists in cache'
114             def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
115             def yangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
116             yangResourceCacheImpl.put(getCacheKey('my-dataspace', 'my-schemaset'), yangTextSchemaSourceSet)
117             def cachedValue = getCachedValue('my-dataspace', 'my-schemaset')
118             assert cachedValue.getModuleReferences() == yangTextSchemaSourceSet.getModuleReferences()
119         when: 'cache is evicted for schemaset'
120             objectUnderTest.removeFromCache('my-dataspace', 'my-schemaset')
121         then: 'cached does not have value'
122             assert getCachedValue('my-dataspace', 'my-schemaset') == null
123         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
124             1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
125     }
126
127     def 'Cache Evict: remove when does not exist'() {
128         given: 'cache is empty'
129             yangResourceCacheImpl.clear()
130         when: 'cache is evicted for schemaset'
131             objectUnderTest.removeFromCache('my-dataspace', 'my-schemaset')
132         then: 'cached does not have value'
133             assert getCachedValue('my-dataspace', 'my-schemaset') == null
134         and: 'the CpsValidator is called on the dataspaceName and schemaSetName'
135             1 * mockCpsValidator.validateNameCharacters('my-dataspace', 'my-schemaset')
136     }
137
138     def getCachedValue(dataSpace, schemaSet) {
139         yangResourceCacheImpl.get(getCacheKey(dataSpace, schemaSet), YangTextSchemaSourceSet)
140     }
141
142     def getCacheKey(dataSpace, schemaSet) {
143         return new String("${dataSpace}-${schemaSet}")
144     }
145
146
147 }