Merge "CM SUBSCRIPTION: Remove accepted and rejected cache entries"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / DmiCmNotificationSubscriptionCacheHandlerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 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  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.cmsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.core.builder.CloudEventBuilder
26 import org.apache.kafka.clients.consumer.ConsumerRecord
27 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.CmNotificationSubscriptionStatus
28 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails
29 import org.onap.cps.ncmp.api.impl.events.cmsubscription.service.CmNotificationSubscriptionPersistenceService
30 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence
31 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
32 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
33 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.CmNotificationSubscriptionNcmpInEvent
34 import org.onap.cps.ncmp.utils.TestUtils
35 import org.onap.cps.utils.JsonObjectMapper
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.boot.test.context.SpringBootTest
39
40 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
41
42 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
43 class DmiCmNotificationSubscriptionCacheHandlerSpec extends MessagingBaseSpec {
44
45     @Autowired
46     JsonObjectMapper jsonObjectMapper
47     @Autowired
48     ObjectMapper objectMapper
49     @SpringBean
50     InventoryPersistence mockInventoryPersistence = Mock(InventoryPersistence)
51     @SpringBean
52     CmNotificationSubscriptionPersistenceService mockCmNotificationSubscriptionPersistenceService = Mock(CmNotificationSubscriptionPersistenceService)
53
54     def testCache = [:]
55     def objectUnderTest = new DmiCmNotificationSubscriptionCacheHandler(mockCmNotificationSubscriptionPersistenceService, testCache, mockInventoryPersistence)
56
57     CmNotificationSubscriptionNcmpInEvent cmNotificationSubscriptionNcmpInEvent
58     def yangModelCmHandle1 = new YangModelCmHandle(id:'ch1',dmiServiceName:'dmi-1')
59     def yangModelCmHandle2 = new YangModelCmHandle(id:'ch2',dmiServiceName:'dmi-2')
60     def yangModelCmHandle3 = new YangModelCmHandle(id:'ch3',dmiServiceName:'dmi-1')
61     def yangModelCmHandle4 = new YangModelCmHandle(id:'ch4',dmiServiceName:'dmi-2')
62
63     def setup() {
64         setUpTestEvent()
65         initialiseMockInventoryPersistenceResponses()
66     }
67
68     def 'Load CM subscription event to cache'() {
69         given: 'a valid subscription event with Id'
70             def subscriptionId = cmNotificationSubscriptionNcmpInEvent.getData().getSubscriptionId()
71         and: 'list of predicates'
72             def predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates()
73         when: 'a valid event object loaded in cache'
74             objectUnderTest.add(subscriptionId, predicates)
75         then: 'the cache contains the correct entry with #subscriptionId subscription ID'
76             assert testCache.containsKey(subscriptionId)
77     }
78
79     def 'Get cache entry via subscription id'() {
80         given: 'the cache contains value for some-id'
81             testCache.put('some-id',[:])
82         when: 'the get method is called'
83             def result = objectUnderTest.get('some-id')
84         then: 'correct value is returned as expected'
85             assert result == [:]
86     }
87
88     def 'Remove accepted and rejected entries from cache via subscription id'() {
89         given: 'a map as the value for cache entry for some-id'
90             def testMap = [:]
91             testMap.put("dmi-1",
92                 new DmiCmNotificationSubscriptionDetails([],CmNotificationSubscriptionStatus.ACCEPTED))
93             testMap.put("dmi-2",
94                 new DmiCmNotificationSubscriptionDetails([],CmNotificationSubscriptionStatus.REJECTED))
95             testMap.put("dmi-3",
96                 new DmiCmNotificationSubscriptionDetails([],CmNotificationSubscriptionStatus.PENDING))
97             testCache.put("test-id", testMap)
98             assert testCache.get("test-id").size() == 3
99         when: 'the method to remove accepted and rejected entries for test-id is called'
100             objectUnderTest.removeAcceptedAndRejectedDmiCmNotificationSubscriptionEntries("test-id")
101         then: 'all entries with status accepted/rejected are no longer present for test-id'
102             testCache.get("test-id").each { key, testResultMap ->
103                 assert testResultMap.cmNotificationSubscriptionStatus != CmNotificationSubscriptionStatus.ACCEPTED
104                     || testResultMap.cmNotificationSubscriptionStatus != CmNotificationSubscriptionStatus.REJECTED
105             }
106         and: 'the size of the map for cache entry test-id is as expected'
107             assert testCache.get("test-id").size() == 1
108     }
109
110     def 'Create map for DMI cm notification subscription per DMI service name'() {
111         given: 'list of predicates from the create subscription event'
112             def predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates()
113         when: 'method to create map of DMI cm notification subscription per DMI service name is called'
114             def result = objectUnderTest.createDmiCmNotificationSubscriptionsPerDmi(predicates)
115         then: 'the result size of resulting map is correct to the number of DMIs'
116             assert result.size() == 2
117         and: 'the cache objects per DMI exists'
118             def resultMapForDmi1 = result.get('dmi-1')
119             def resultMapForDmi2 = result.get('dmi-2')
120             assert resultMapForDmi1 != null
121             assert resultMapForDmi2 != null
122         and: 'the size of predicates in each object is correct'
123             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates.size() == 2
124             assert resultMapForDmi2.dmiCmNotificationSubscriptionPredicates.size() == 2
125         and: 'the subscription status in each object is correct'
126             assert resultMapForDmi1.cmNotificationSubscriptionStatus.toString() == 'PENDING'
127             assert resultMapForDmi2.cmNotificationSubscriptionStatus.toString() == 'PENDING'
128         and: 'the target cmHandles for each predicate is correct'
129             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[0].targetCmHandleIds == ['ch1'].toSet()
130             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[1].targetCmHandleIds == ['ch3'].toSet()
131
132             assert resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[0].targetCmHandleIds == ['ch2'].toSet()
133             assert resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[1].targetCmHandleIds == ['ch4'].toSet()
134         and: 'the list of xpath for each is correct'
135             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[0].xpaths
136                     && resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[0].xpaths == ['/x1/y1','x2/y2'].toSet()
137
138             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[1].xpaths
139                     && resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[1].xpaths == ['/x3/y3','x4/y4'].toSet()
140     }
141
142     def 'Get map for cm handle IDs by DMI service name'() {
143         given: 'the predicate from the test request CM subscription event'
144             def targetFilter = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates().get(0).getTargetFilter()
145         when: 'the method to group all target CM handles by DMI service name is called'
146             def mapOfCMHandleIDsByDmi = objectUnderTest.groupTargetCmHandleIdsByDmi(targetFilter)
147         then: 'the size of the resulting map is correct'
148             assert mapOfCMHandleIDsByDmi.size() == 2
149         and: 'the values in the map is as expected'
150             assert mapOfCMHandleIDsByDmi.get('dmi-1') == ['ch1'].toSet()
151             assert mapOfCMHandleIDsByDmi.get('dmi-2') == ['ch2'].toSet()
152     }
153
154     def 'Update subscription status in cache per DMI service name'() {
155         given: 'populated cache'
156             def predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates()
157             def subscriptionId = cmNotificationSubscriptionNcmpInEvent.getData().getSubscriptionId()
158             objectUnderTest.add(subscriptionId, predicates)
159         when: 'subscription status per dmi is updated in cache'
160             objectUnderTest.updateDmiCmNotificationSubscriptionStatusPerDmi(subscriptionId,'dmi-1', CmNotificationSubscriptionStatus.ACCEPTED)
161         then: 'verify status has been updated in cache'
162             def predicate = testCache.get(subscriptionId)
163             assert predicate.get('dmi-1').cmNotificationSubscriptionStatus == CmNotificationSubscriptionStatus.ACCEPTED
164     }
165
166     def 'Persist Cache into database per dmi'() {
167         given: 'populate cache'
168             def predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates()
169             def subscriptionId = cmNotificationSubscriptionNcmpInEvent.getData().getSubscriptionId()
170             objectUnderTest.add(subscriptionId, predicates)
171         when: 'subscription is persisted in database'
172             objectUnderTest.persistIntoDatabasePerDmi(subscriptionId,'dmi-1')
173         then: 'persistence service is called the correct number of times per dmi'
174             4 * mockCmNotificationSubscriptionPersistenceService.addCmNotificationSubscription(_,_,_,subscriptionId)
175     }
176
177     def setUpTestEvent(){
178         def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
179         def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
180         def testCloudEventSent = CloudEventBuilder.v1()
181                 .withData(objectMapper.writeValueAsBytes(testEventSent))
182                 .withId('subscriptionCreated')
183                 .withType('subscriptionCreated')
184                 .withSource(URI.create('some-resource'))
185                 .withExtension('correlationid', 'test-cmhandle1').build()
186         def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
187         def cloudEvent = consumerRecord.value()
188
189         cmNotificationSubscriptionNcmpInEvent = toTargetEvent(cloudEvent, CmNotificationSubscriptionNcmpInEvent.class);
190     }
191
192     def initialiseMockInventoryPersistenceResponses(){
193         mockInventoryPersistence.getYangModelCmHandles(['ch1','ch2'])
194                 >> [yangModelCmHandle1, yangModelCmHandle2]
195
196         mockInventoryPersistence.getYangModelCmHandles(['ch3','ch4'])
197                 >> [yangModelCmHandle3, yangModelCmHandle4]
198     }
199
200 }