Subscription Create Event Outcome Kafka Part
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / event / avc / 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.event.avc
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.events.avcsubscription.SubscriptionEventResponseMapper
27 import org.onap.cps.ncmp.api.impl.events.avcsubscription.SubscriptionEventResponseOutcome
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.api.models.SubscriptionEventResponse
31 import org.onap.cps.utils.JsonObjectMapper
32 import org.springframework.boot.test.context.SpringBootTest
33
34 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
35 class SubscriptionEventResponseConsumerSpec extends MessagingBaseSpec {
36
37     IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
38     def mockSubscriptionPersistence = Mock(SubscriptionPersistenceImpl)
39     def mockSubscriptionEventResponseMapper  = Mock(SubscriptionEventResponseMapper)
40     def mockSubscriptionEventResponseOutcome = Mock(SubscriptionEventResponseOutcome)
41
42     def objectUnderTest = new SubscriptionEventResponseConsumer(mockForwardedSubscriptionEventCache,
43         mockSubscriptionPersistence, mockSubscriptionEventResponseMapper, mockSubscriptionEventResponseOutcome)
44
45     def cmHandleToStatusMap = [CMHandle1: 'PENDING', CMHandle2: 'ACCEPTED'] as Map
46     def testEventReceived = new SubscriptionEventResponse(clientId: 'some-client-id',
47         subscriptionName: 'some-subscription-name', dmiName: 'some-dmi-name', cmHandleIdToStatus: cmHandleToStatusMap)
48     def consumerRecord = new ConsumerRecord<String, SubscriptionEventResponse>('topic-name', 0, 0, 'event-key', testEventReceived)
49
50     def 'Consume Subscription Event Response where all DMIs have responded'() {
51         given: 'a subscription event response and notifications are enabled'
52             objectUnderTest.notificationFeatureEnabled = true
53         and: 'subscription model loader is enabled'
54             objectUnderTest.subscriptionModelLoaderEnabled = true
55         when: 'the valid event is consumed'
56             objectUnderTest.consumeSubscriptionEventResponse(consumerRecord)
57         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
58             1 * mockForwardedSubscriptionEventCache.containsKey('some-client-idsome-subscription-name') >> true
59             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['some-dmi-name'] as Set)
60         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
61             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> ([] as Set)
62         and: 'the subscription event is removed from the map'
63             1 * mockForwardedSubscriptionEventCache.remove('some-client-idsome-subscription-name')
64         and: 'a response outcome has been created'
65             1 * mockSubscriptionEventResponseOutcome.sendResponse('some-client-id', 'some-subscription-name', true)
66     }
67
68     def 'Consume Subscription Event Response where another DMI has not yet responded'() {
69         given: 'a subscription event response and notifications are enabled'
70             objectUnderTest.notificationFeatureEnabled = true
71         and: 'subscription model loader is enabled'
72             objectUnderTest.subscriptionModelLoaderEnabled = true
73         when: 'the valid event is consumed'
74             objectUnderTest.consumeSubscriptionEventResponse(consumerRecord)
75         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
76             1 * mockForwardedSubscriptionEventCache.containsKey('some-client-idsome-subscription-name') >> true
77             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['some-dmi-name', 'non-responded-dmi'] as Set)
78         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
79             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['non-responded-dmi'] as Set)
80         and: 'the subscription event is not removed from the map'
81             0 * mockForwardedSubscriptionEventCache.remove(_)
82         and: 'a response outcome has not been created'
83             0 * mockSubscriptionEventResponseOutcome.sendResponse(*_)
84     }
85 }