NCMP : Handle non-existing and non-ready cm handles
[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 org.apache.kafka.clients.consumer.ConsumerRecord
26 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistenceImpl
27 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
28 import org.onap.cps.ncmp.api.models.SubscriptionEventResponse
29 import org.onap.cps.utils.JsonObjectMapper
30 import org.springframework.boot.test.context.SpringBootTest
31
32 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
33 class SubscriptionEventResponseConsumerSpec extends MessagingBaseSpec {
34
35     IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
36     def mockSubscriptionPersistence = Mock(SubscriptionPersistenceImpl)
37     def mockSubscriptionEventResponseMapper  = Mock(SubscriptionEventResponseMapper)
38     def mockSubscriptionEventResponseOutcome = Mock(SubscriptionEventResponseOutcome)
39
40     def objectUnderTest = new SubscriptionEventResponseConsumer(mockForwardedSubscriptionEventCache,
41         mockSubscriptionPersistence, mockSubscriptionEventResponseMapper, mockSubscriptionEventResponseOutcome)
42
43     def cmHandleToStatusMap = [CMHandle1: 'PENDING', CMHandle2: 'ACCEPTED'] as Map
44     def testEventReceived = new SubscriptionEventResponse(clientId: 'some-client-id',
45         subscriptionName: 'some-subscription-name', dmiName: 'some-dmi-name', cmHandleIdToStatus: cmHandleToStatusMap)
46     def consumerRecord = new ConsumerRecord<String, SubscriptionEventResponse>('topic-name', 0, 0, 'event-key', testEventReceived)
47
48     def 'Consume Subscription Event Response where all DMIs have responded'() {
49         given: 'a subscription event response and notifications are enabled'
50             objectUnderTest.notificationFeatureEnabled = isNotificationFeatureEnabled
51         and: 'subscription model loader is enabled'
52             objectUnderTest.subscriptionModelLoaderEnabled = true
53         when: 'the valid event is consumed'
54             objectUnderTest.consumeSubscriptionEventResponse(consumerRecord)
55         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
56             1 * mockForwardedSubscriptionEventCache.containsKey('some-client-idsome-subscription-name') >> true
57             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['some-dmi-name'] as Set)
58         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
59             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> ([] as Set)
60         and: 'the subscription event is removed from the map'
61             1 * mockForwardedSubscriptionEventCache.remove('some-client-idsome-subscription-name')
62         and: 'a response outcome has been created'
63             numberOfExpectedCallToSendResponse * mockSubscriptionEventResponseOutcome.sendResponse('some-client-id', 'some-subscription-name', isFullOutcomeResponse)
64         where: 'the following values are used'
65             scenario             | isNotificationFeatureEnabled | isFullOutcomeResponse             || numberOfExpectedCallToSendResponse
66             'Response sent'      | true                         | true                              || 1
67             'Response not sent'  | true                         | false                             || 0
68             'Response not sent'  | false                        | true                              || 0
69             'Response not sent'  | false                        | false                             || 0
70     }
71
72     def 'Consume Subscription Event Response where another DMI has not yet responded'() {
73         given: 'a subscription event response and notifications are enabled'
74             objectUnderTest.notificationFeatureEnabled = true
75         and: 'subscription model loader is enabled'
76             objectUnderTest.subscriptionModelLoaderEnabled = true
77         when: 'the valid event is consumed'
78             objectUnderTest.consumeSubscriptionEventResponse(consumerRecord)
79         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
80             1 * mockForwardedSubscriptionEventCache.containsKey('some-client-idsome-subscription-name') >> true
81             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['some-dmi-name', 'non-responded-dmi'] as Set)
82         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
83             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['non-responded-dmi'] as Set)
84         and: 'the subscription event is not removed from the map'
85             0 * mockForwardedSubscriptionEventCache.remove(_)
86         and: 'a response outcome has not been created'
87             0 * mockSubscriptionEventResponseOutcome.sendResponse(*_)
88     }
89
90     def 'Update subscription event when the model loader flag is enabled'() {
91         given: 'subscription model loader is enabled as per #scenario'
92             objectUnderTest.subscriptionModelLoaderEnabled = isSubscriptionModelLoaderEnabled
93         when: 'the valid event is consumed'
94             objectUnderTest.consumeSubscriptionEventResponse(consumerRecord)
95         then: 'the forwarded subscription event cache does not return dmiName for the subscription create event'
96             1 * mockForwardedSubscriptionEventCache.containsKey('some-client-idsome-subscription-name') >> false
97         and: 'the mapper returns yang model subscription event with #numberOfExpectedCall'
98             numberOfExpectedCall * mockSubscriptionEventResponseMapper.toYangModelSubscriptionEvent(_)
99         and: 'subscription event has been updated into DB with #numberOfExpectedCall'
100             numberOfExpectedCall * mockSubscriptionPersistence.saveSubscriptionEvent(_)
101         where: 'the following values are used'
102             scenario                   | isSubscriptionModelLoaderEnabled || numberOfExpectedCall
103             'The event is updated'     | true                             || 1
104             'The event is not updated' | false                            || 0
105     }
106 }