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