Merge "Populate cmSubscription cache"
[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.inventory.InventoryPersistence
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
30 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.CmNotificationSubscriptionNcmpInEvent
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36
37 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
38
39 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
40 class DmiCmNotificationSubscriptionCacheHandlerSpec extends MessagingBaseSpec {
41
42     @Autowired
43     JsonObjectMapper jsonObjectMapper
44     @Autowired
45     ObjectMapper objectMapper
46     @SpringBean
47     InventoryPersistence mockInventoryPersistence = Mock(InventoryPersistence)
48
49     def testCache = [:]
50     def objectUnderTest = new DmiCmNotificationSubscriptionCacheHandler(testCache, mockInventoryPersistence)
51
52     CmNotificationSubscriptionNcmpInEvent cmNotificationSubscriptionNcmpInEvent
53     def yangModelCmHandle1 = new YangModelCmHandle(id:'ch1',dmiServiceName:'dmi-1')
54     def yangModelCmHandle2 = new YangModelCmHandle(id:'ch2',dmiServiceName:'dmi-2')
55     def yangModelCmHandle3 = new YangModelCmHandle(id:'ch3',dmiServiceName:'dmi-1')
56     def yangModelCmHandle4 = new YangModelCmHandle(id:'ch4',dmiServiceName:'dmi-2')
57
58     def setup() {
59         setUpTestEvent()
60         initialiseMockInventoryPersistenceResponses()
61     }
62
63     def 'Load CM subscription event to cache'() {
64         given: 'a valid subscription event with Id'
65             def subscriptionId = cmNotificationSubscriptionNcmpInEvent.getData().getSubscriptionId();
66         and: 'list of predicates'
67             def predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates();
68         when: 'a valid event object loaded in cache'
69             objectUnderTest.add(subscriptionId, predicates)
70         then: 'the cache contains the correct entry with #subscriptionId subscription ID'
71             assert testCache.containsKey(subscriptionId)
72     }
73
74     def 'Create map for DMI cm notification subscription per DMI service name'() {
75         given: 'list of predicates from the create subscription event'
76             def predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates()
77         when: 'method to create map of DMI cm notification subscription per DMI service name is called'
78             def result = objectUnderTest.createDmiCmNotificationSubscriptionsPerDmi(predicates)
79         then: 'the result size of resulting map is correct to the number of DMIs'
80             assert result.size() == 2
81         and: 'the cache objects per DMI exists'
82             def resultMapForDmi1 = result.get('dmi-1')
83             def resultMapForDmi2 = result.get('dmi-2')
84             assert resultMapForDmi1 != null
85             assert resultMapForDmi2 != null
86         and: 'the size of predicates in each object is correct'
87             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates.size() == 2
88             assert resultMapForDmi2.dmiCmNotificationSubscriptionPredicates.size() == 2
89         and: 'the subscription status in each object is correct'
90             assert resultMapForDmi1.cmNotificationSubscriptionStatus.toString() == 'PENDING'
91             assert resultMapForDmi2.cmNotificationSubscriptionStatus.toString() == 'PENDING'
92         and: 'the target cmHandles for each predicate is correct'
93             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[0].targetCmHandleIds == ['ch1'].toSet()
94             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[1].targetCmHandleIds == ['ch3'].toSet()
95
96             assert resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[0].targetCmHandleIds == ['ch2'].toSet()
97             assert resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[1].targetCmHandleIds == ['ch4'].toSet()
98         and: 'the list of xpath for each is correct'
99             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[0].xpaths
100                     && resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[0].xpaths == ['/x1/y1','x2/y2'].toSet()
101
102             assert resultMapForDmi1.dmiCmNotificationSubscriptionPredicates[1].xpaths
103                     && resultMapForDmi2.dmiCmNotificationSubscriptionPredicates[1].xpaths == ['/x3/y3','x4/y4'].toSet()
104     }
105
106     def 'Get map for cm handle IDs by DMI service name'() {
107         given: 'the predicate from the test request CM subscription event'
108             def targetFilter = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates().get(0).getTargetFilter()
109         when: 'the method to group all target CM handles by DMI service name is called'
110             def mapOfCMHandleIDsByDmi = objectUnderTest.groupTargetCmHandleIdsByDmi(targetFilter)
111         then: 'the size of the resulting map is correct'
112             assert mapOfCMHandleIDsByDmi.size() == 2
113         and: 'the values in the map is as expected'
114             assert mapOfCMHandleIDsByDmi.get('dmi-1') == ['ch1'].toSet()
115             assert mapOfCMHandleIDsByDmi.get('dmi-2') == ['ch2'].toSet()
116     }
117
118     def setUpTestEvent(){
119         def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
120         def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
121         def testCloudEventSent = CloudEventBuilder.v1()
122                 .withData(objectMapper.writeValueAsBytes(testEventSent))
123                 .withId('subscriptionCreated')
124                 .withType('subscriptionCreated')
125                 .withSource(URI.create('some-resource'))
126                 .withExtension('correlationid', 'test-cmhandle1').build()
127         def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
128         def cloudEvent = consumerRecord.value()
129
130         cmNotificationSubscriptionNcmpInEvent = toTargetEvent(cloudEvent, CmNotificationSubscriptionNcmpInEvent.class);
131     }
132
133     def initialiseMockInventoryPersistenceResponses(){
134         mockInventoryPersistence.getYangModelCmHandles(['ch1','ch2'])
135                 >> [yangModelCmHandle1, yangModelCmHandle2]
136
137         mockInventoryPersistence.getYangModelCmHandles(['ch3','ch4'])
138                 >> [yangModelCmHandle3, yangModelCmHandle4]
139     }
140
141 }