a673462008ca31fadea34f4c35f73184e2c5548e
[cps.git] /
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.onap.cps.ncmp.api.kafka.MessagingBaseSpec
26 import org.onap.cps.ncmp.api.models.SubscriptionEventResponse
27 import org.onap.cps.utils.JsonObjectMapper
28 import org.springframework.boot.test.context.SpringBootTest
29
30 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
31 class SubscriptionEventResponseConsumerSpec extends MessagingBaseSpec {
32
33     IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>)
34
35     def objectUnderTest = new SubscriptionEventResponseConsumer(mockForwardedSubscriptionEventCache)
36
37
38     def 'Consume Subscription Event Response where all DMIs have responded'() {
39         given: 'a subscription event response with a clientId, subscriptionName and dmiName'
40             def testEventReceived = new SubscriptionEventResponse()
41             testEventReceived.clientId = 'some-client-id'
42             testEventReceived.subscriptionName = 'some-subscription-name'
43             testEventReceived.dmiName = 'some-dmi-name'
44         and: 'notifications are enabled'
45             objectUnderTest.notificationFeatureEnabled = true
46         and: 'subscription model loader is enabled'
47             objectUnderTest.subscriptionModelLoaderEnabled = true
48         when: 'the valid event is consumed'
49             objectUnderTest.consumeSubscriptionEventResponse(testEventReceived)
50         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
51             1 * mockForwardedSubscriptionEventCache.containsKey('some-client-idsome-subscription-name') >> true
52             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['some-dmi-name'] as Set)
53         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
54             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> ([] as Set)
55         and: 'the subscription event is removed from the map'
56             1 * mockForwardedSubscriptionEventCache.remove('some-client-idsome-subscription-name')
57     }
58
59     def 'Consume Subscription Event Response where another DMI has not yet responded'() {
60         given: 'a subscription event response with a clientId, subscriptionName and dmiName'
61             def testEventReceived = new SubscriptionEventResponse()
62             testEventReceived.clientId = 'some-client-id'
63             testEventReceived.subscriptionName = 'some-subscription-name'
64             testEventReceived.dmiName = 'some-dmi-name'
65         and: 'notifications are enabled'
66             objectUnderTest.notificationFeatureEnabled = true
67         and: 'subscription model loader is enabled'
68             objectUnderTest.subscriptionModelLoaderEnabled = true
69         when: 'the valid event is consumed'
70             objectUnderTest.consumeSubscriptionEventResponse(testEventReceived)
71         then: 'the forwarded subscription event cache returns only the received dmiName existing for the subscription create event'
72             1 * mockForwardedSubscriptionEventCache.containsKey('some-client-idsome-subscription-name') >> true
73             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['some-dmi-name', 'non-responded-dmi'] as Set)
74         and: 'the forwarded subscription event cache returns an empty Map when the dmiName has been removed'
75             1 * mockForwardedSubscriptionEventCache.get('some-client-idsome-subscription-name') >> (['non-responded-dmi'] as Set)
76         and: 'the subscription event is not removed from the map'
77             0 * mockForwardedSubscriptionEventCache.remove(_)
78     }
79 }