Merge "Improve 32K limit tests"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / avcsubscription / SubscriptionEventResponseConsumerSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (c) 2023 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.avcsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import com.hazelcast.map.IMap
25 import io.cloudevents.CloudEvent
26 import io.cloudevents.core.builder.CloudEventBuilder
27 import org.apache.kafka.clients.consumer.ConsumerRecord
28 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistenceImpl
29 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
30 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.spi.model.DataNodeBuilder
33 import org.onap.cps.utils.JsonObjectMapper
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
36
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
38 class SubscriptionEventResponseConsumerSpec extends MessagingBaseSpec {
39
40     @Autowired
41     JsonObjectMapper jsonObjectMapper
42
43     @Autowired
44     ObjectMapper objectMapper
45
46     IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
47     def mockSubscriptionPersistence = Mock(SubscriptionPersistenceImpl)
48     def mockSubscriptionEventResponseMapper  = Mock(SubscriptionEventResponseMapper)
49     def mockSubscriptionEventResponseOutcome = Mock(SubscriptionEventResponseOutcome)
50
51     def objectUnderTest = new SubscriptionEventResponseConsumer(mockForwardedSubscriptionEventCache,
52         mockSubscriptionPersistence, mockSubscriptionEventResponseMapper, mockSubscriptionEventResponseOutcome)
53
54     def 'Consume Subscription Event Response where all DMIs have responded'() {
55         given: 'a consumer record including cloud event having subscription response'
56             def consumerRecordWithCloudEventAndSubscriptionResponse = getConsumerRecord()
57         and: 'a subscription response event'
58             def subscriptionResponseEvent = getSubscriptionResponseEvent()
59         and: 'a subscription event response and notifications are enabled'
60             objectUnderTest.notificationFeatureEnabled = notificationEnabled
61         and: 'subscription model loader is enabled'
62             objectUnderTest.subscriptionModelLoaderEnabled = modelLoaderEnabled
63         and: 'subscription persistence service returns data node includes no pending cm handle'
64             mockSubscriptionPersistence.getCmHandlesForSubscriptionEvent(*_) >> [getDataNode()]
65         when: 'the valid event is consumed'
66             objectUnderTest.consumeSubscriptionEventResponse(consumerRecordWithCloudEventAndSubscriptionResponse)
67         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
68             1 * mockForwardedSubscriptionEventCache.containsKey('SCO-9989752cm-subscription-001') >> true
69             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['some-dmi-name'] as Set)
70         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
71             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> ([] as Set)
72         and: 'the response event is map to yang model'
73             numberOfTimeToPersist * mockSubscriptionEventResponseMapper.toYangModelSubscriptionEvent(_)
74         and: 'the response event is persisted into the db'
75             numberOfTimeToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(_)
76         and: 'the subscription event is removed from the map'
77             numberOfTimeToRemove * mockForwardedSubscriptionEventCache.remove('SCO-9989752cm-subscription-001')
78         and: 'a response outcome has been created'
79             numberOfTimeToResponse * mockSubscriptionEventResponseOutcome.sendResponse(subscriptionResponseEvent, 'subscriptionCreated')
80         where: 'the following values are used'
81             scenario                                              | modelLoaderEnabled  |   notificationEnabled  ||  numberOfTimeToPersist  ||  numberOfTimeToRemove  || numberOfTimeToResponse
82             'Both model loader and notification are enabled'      |    true             |     true               ||   1                     ||      1                 ||       1
83             'Both model loader and notification are disabled'     |    false            |     false              ||   0                     ||      0                 ||       0
84             'Model loader enabled and notification  disabled'     |    true             |     false              ||   1                     ||      0                 ||       0
85             'Model loader disabled and notification enabled'      |    false            |     true               ||   0                     ||      1                 ||       1
86     }
87
88     def 'Consume Subscription Event Response where another DMI has not yet responded'() {
89         given: 'a subscription event response and notifications are enabled'
90             objectUnderTest.notificationFeatureEnabled = notificationEnabled
91         and: 'subscription model loader is enabled'
92             objectUnderTest.subscriptionModelLoaderEnabled = modelLoaderEnabled
93         when: 'the valid event is consumed'
94             objectUnderTest.consumeSubscriptionEventResponse(getConsumerRecord())
95         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
96             1 * mockForwardedSubscriptionEventCache.containsKey('SCO-9989752cm-subscription-001') >> true
97             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['responded-dmi', 'non-responded-dmi'] as Set)
98         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
99             1 * mockForwardedSubscriptionEventCache.get('SCO-9989752cm-subscription-001') >> (['non-responded-dmi'] as Set)
100         and: 'the response event is map to yang model'
101             numberOfTimeToPersist * mockSubscriptionEventResponseMapper.toYangModelSubscriptionEvent(_)
102         and: 'the response event is persisted into the db'
103             numberOfTimeToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(_)
104         and: 'the subscription event is removed from the map'
105         and: 'the subscription event is not removed from the map'
106             0 * mockForwardedSubscriptionEventCache.remove(_)
107         and: 'a response outcome has not been created'
108             0 * mockSubscriptionEventResponseOutcome.sendResponse(*_)
109         where: 'the following values are used'
110             scenario                                              | modelLoaderEnabled  |   notificationEnabled  ||  numberOfTimeToPersist
111             'Both model loader and notification are enabled'      |    true             |     true               ||   1
112             'Both model loader and notification are disabled'     |    false            |     false              ||   0
113             'Model loader enabled and notification  disabled'     |    true             |     false              ||   1
114             'Model loader disabled and notification enabled'      |    false            |     true               ||   0
115     }
116
117     def getSubscriptionResponseEvent() {
118         def subscriptionResponseJsonData = TestUtils.getResourceFileContent('avcSubscriptionEventResponse.json')
119         return jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, SubscriptionEventResponse.class)
120     }
121
122     def getCloudEventHavingSubscriptionResponseEvent() {
123         return CloudEventBuilder.v1()
124             .withData(objectMapper.writeValueAsBytes(getSubscriptionResponseEvent()))
125             .withId('some-id')
126             .withType('subscriptionCreated')
127             .withSource(URI.create('NCMP')).build()
128     }
129
130     def getConsumerRecord() {
131         return new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', getCloudEventHavingSubscriptionResponseEvent())
132     }
133
134     def getDataNode() {
135         def leaves = [status:'ACCEPTED', cmHandleId:'cmhandle1'] as Map
136         return new DataNodeBuilder().withDataspace('NCMP-Admin')
137             .withAnchor('AVC-Subscriptions').withXpath('/subscription-registry/subscription')
138             .withLeaves(leaves).build()
139     }
140 }