2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Bell Canada
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
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.api.impl
23 import org.onap.cps.TestUtils
24 import org.onap.cps.spi.CpsModulePersistenceService
25 import org.onap.cps.yang.YangTextSchemaSourceSet
26 import org.onap.cps.yang.YangTextSchemaSourceSetBuilder
27 import org.spockframework.spring.SpringBean
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.test.context.SpringBootTest
30 import org.springframework.cache.Cache
31 import org.springframework.cache.CacheManager
32 import org.springframework.cache.annotation.EnableCaching
33 import org.springframework.cache.caffeine.CaffeineCacheManager
34 import org.springframework.test.context.ContextConfiguration
35 import spock.lang.Specification
39 @ContextConfiguration(classes = [YangTextSchemaSourceSetCache, CaffeineCacheManager])
40 class YangTextSchemaSourceSetCacheSpec extends Specification {
43 CpsModulePersistenceService mockModuleStoreService = Mock()
46 YangTextSchemaSourceSetCache objectUnderTest
49 CacheManager cacheManager
51 Cache yangResourceCacheImpl;
54 yangResourceCacheImpl = cacheManager.getCache('yangSchema')
55 yangResourceCacheImpl.clear()
59 def 'Cache Miss: Fetch data from Module persistence'() {
60 given: 'cache is empty'
61 yangResourceCacheImpl.clear()
62 and: 'a schema set exists'
63 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
64 def expectedYangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
65 when: 'schema-set information is asked'
66 def result = objectUnderTest.get('my-dataspace', 'my-schemaset')
67 then: 'information fetched from cps module persistence'
68 1 * mockModuleStoreService.getYangSchemaResources('my-dataspace', 'my-schemaset')
69 >> yangResourcesNameToContentMap
70 and: 'stored in the cache'
71 def cachedValue = getCachedValue('my-dataspace', 'my-schemaset')
72 assert cachedValue.getModuleReferences() == expectedYangTextSchemaSourceSet.getModuleReferences()
73 and: 'the response is as expected'
74 assert result.getModuleReferences() == expectedYangTextSchemaSourceSet.getModuleReferences()
77 def 'Cache Hit: Respond from cache'() {
78 given: 'a schema set exists'
79 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
80 def expectedYangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
81 and: 'stored in cache'
82 yangResourceCacheImpl.put(getCacheKey('my-dataspace', 'my-schemaset'), expectedYangTextSchemaSourceSet)
83 when: 'schema-set information is asked'
84 def result = objectUnderTest.get('my-dataspace', 'my-schemaset')
85 then: 'expected value is returned'
86 result.getModuleReferences() == expectedYangTextSchemaSourceSet.getModuleReferences()
87 and: 'module persistence is not invoked'
88 0 * mockModuleStoreService.getYangSchemaResources(_, _)
91 def 'Cache Update: when no data exist in the cache'() {
92 given: 'a schema set exists'
93 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
94 def yangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
95 when: 'cache is updated'
96 objectUnderTest.updateCache('my-dataspace', 'my-schemaset', yangTextSchemaSourceSet)
97 then: 'cached value is same as expected'
98 def cachedValue = getCachedValue('my-dataspace', 'my-schemaset')
99 cachedValue.getModuleReferences() == yangTextSchemaSourceSet.getModuleReferences()
102 def 'Cache Evict: remove when exist'() {
103 given: 'a schema set exists in cache'
104 def yangResourcesNameToContentMap = TestUtils.getYangResourcesAsMap('bookstore.yang')
105 def yangTextSchemaSourceSet = YangTextSchemaSourceSetBuilder.of(yangResourcesNameToContentMap)
106 yangResourceCacheImpl.put(getCacheKey('my-dataspace', 'my-schemaset'), yangTextSchemaSourceSet)
107 def cachedValue = getCachedValue('my-dataspace', 'my-schemaset')
108 assert cachedValue.getModuleReferences() == yangTextSchemaSourceSet.getModuleReferences()
109 when: 'cache is evicted for schemaset'
110 objectUnderTest.removeFromCache('my-dataspace', 'my-schemaset')
111 then: 'cached does not have value'
112 assert getCachedValue('my-dataspace', 'my-schemaset') == null
115 def 'Cache Evict: remove when does not exist'() {
116 given: 'cache is empty'
117 yangResourceCacheImpl.clear()
118 when: 'cache is evicted for schemaset'
119 objectUnderTest.removeFromCache('my-dataspace', 'my-schemaset')
120 then: 'cached does not have value'
121 assert getCachedValue('my-dataspace', 'my-schemaset') == null
124 def getCachedValue(dataSpace, schemaSet) {
125 yangResourceCacheImpl.get(getCacheKey(dataSpace, schemaSet), YangTextSchemaSourceSet)
128 def getCacheKey(dataSpace, schemaSet) {
129 return new String("${dataSpace}-${schemaSet}")