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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.impl.cmnotificationsubscription.ncmp
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.ncmp.impl.cmnotificationsubscription.cache.DmiCacheHandler
25 import org.onap.cps.ncmp.impl.cmnotificationsubscription.dmi.DmiInEventMapper
26 import org.onap.cps.ncmp.impl.cmnotificationsubscription.dmi.DmiInEventProducer
27 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionDetails
28 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionPredicate
29 import org.onap.cps.ncmp.impl.cmnotificationsubscription.utils.CmSubscriptionPersistenceService
30 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.client_to_ncmp.NcmpInEvent
31 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent
32 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_dmi.DmiInEvent
33 import org.onap.cps.ncmp.utils.TestUtils
34 import org.onap.cps.utils.JsonObjectMapper
35 import spock.lang.Specification
37 import static org.onap.cps.ncmp.api.data.models.DatastoreType.PASSTHROUGH_OPERATIONAL
38 import static org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus.ACCEPTED
39 import static org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus.PENDING
41 class CmSubscriptionHandlerImplSpec extends Specification {
43 def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
44 def mockCmSubscriptionPersistenceService = Mock(CmSubscriptionPersistenceService)
45 def mockCmSubscriptionComparator = Mock(CmSubscriptionComparator)
46 def mockNcmpOutEventMapper = Mock(NcmpOutEventMapper)
47 def mockDmiInEventMapper = Mock(DmiInEventMapper)
48 def mockNcmpOutEventProducer = Mock(NcmpOutEventProducer)
49 def mockDmiInEventProducer = Mock(DmiInEventProducer)
50 def mockDmiCacheHandler = Mock(DmiCacheHandler)
52 def objectUnderTest = new CmSubscriptionHandlerImpl(mockCmSubscriptionPersistenceService,
53 mockCmSubscriptionComparator, mockNcmpOutEventMapper, mockDmiInEventMapper,
54 mockNcmpOutEventProducer, mockDmiInEventProducer, mockDmiCacheHandler)
56 def testDmiSubscriptionsPerDmi = ["dmi-1": new DmiCmSubscriptionDetails([], PENDING)]
58 def 'Consume valid and unique CmNotificationSubscriptionNcmpInEvent create message'() {
59 given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
60 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
61 def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
62 def testListOfDeltaPredicates = [new DmiCmSubscriptionPredicate(['ch1'].toSet(), PASSTHROUGH_OPERATIONAL, ['/a/b'].toSet())]
63 mockCmSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
64 and: 'relevant details is extracted from the event'
65 def subscriptionId = testEventConsumed.getData().getSubscriptionId()
66 def predicates = testEventConsumed.getData().getPredicates()
67 and: 'the cache handler returns for relevant subscription id'
68 1 * mockDmiCacheHandler.get("test-id") >> testDmiSubscriptionsPerDmi
69 and: 'the delta predicates is returned'
70 1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> testListOfDeltaPredicates
71 and: 'the DMI in event mapper returns cm notification subscription event'
72 def testDmiInEvent = new DmiInEvent()
73 1 * mockDmiInEventMapper.toDmiInEvent(testListOfDeltaPredicates) >> testDmiInEvent
74 when: 'the valid and unique event is consumed'
75 objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
76 then: 'the subscription cache handler is called once'
77 1 * mockDmiCacheHandler.add('test-id', _)
78 and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
79 testDmiSubscriptionsPerDmi.size() * mockDmiInEventProducer.publishDmiInEvent(
80 "test-id", "dmi-1", "subscriptionCreateRequest", testDmiInEvent)
81 and: 'we schedule to send the response after configured time from the cache'
82 1 * mockNcmpOutEventProducer.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
85 def 'Consume valid and Overlapping Cm Notification Subscription NcmpIn Event'() {
86 given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
87 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
88 def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
89 def noDeltaPredicates = []
90 mockCmSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
91 and: 'the cache handler returns for relevant subscription id'
92 1 * mockDmiCacheHandler.get('test-id') >> testDmiSubscriptionsPerDmi
93 and: 'the delta predicates is returned'
94 1 * mockCmSubscriptionComparator.getNewDmiSubscriptionPredicates(_) >> noDeltaPredicates
95 when: 'the valid and unique event is consumed'
96 objectUnderTest.processSubscriptionCreateRequest('test-id', noDeltaPredicates)
97 then: 'the subscription cache handler is called once'
98 1 * mockDmiCacheHandler.add('test-id', _)
99 and: 'the subscription details are updated in the cache'
100 1 * mockDmiCacheHandler.updateDmiSubscriptionStatusPerDmi('test-id', _, ACCEPTED)
101 and: 'we schedule to send the response after configured time from the cache'
102 1 * mockNcmpOutEventProducer.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', null, true)
105 def 'Consume valid and but non-unique CmNotificationSubscription create message'() {
106 given: 'a cmNotificationSubscriptionNcmp in event'
107 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
108 def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
109 mockCmSubscriptionPersistenceService.isUniqueSubscriptionId('test-id') >> false
110 and: 'relevant details is extracted from the event'
111 def subscriptionId = testEventConsumed.getData().getSubscriptionId()
112 def predicates = testEventConsumed.getData().getPredicates()
113 and: 'the NCMP out in event mapper returns an event for rejected request'
114 def testNcmpOutEvent = new NcmpOutEvent()
115 1 * mockNcmpOutEventMapper.toNcmpOutEventForRejectedRequest(
116 "test-id", _) >> testNcmpOutEvent
117 when: 'the valid but non-unique event is consumed'
118 objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
119 then: 'the events handler method to publish DMI event is never called'
120 0 * mockDmiInEventProducer.publishDmiInEvent(_, _, _, _)
121 and: 'the events handler method to publish NCMP out event is called once'
122 1 * mockNcmpOutEventProducer.publishNcmpOutEvent('test-id', 'subscriptionCreateResponse', testNcmpOutEvent, false)
125 def 'Consume valid CmNotificationSubscriptionNcmpInEvent delete message'() {
126 given: 'a cmNotificationSubscriptionNcmp in event for delete'
127 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
128 def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, NcmpInEvent.class)
129 and: 'relevant details is extracted from the event'
130 def subscriptionId = testEventConsumed.getData().getSubscriptionId()
131 def predicates = testEventConsumed.getData().getPredicates()
132 and: 'the cache handler returns for relevant subscription id'
133 1 * mockDmiCacheHandler.get('test-id') >> testDmiSubscriptionsPerDmi
134 when: 'the valid and unique event is consumed'
135 objectUnderTest.processSubscriptionDeleteRequest(subscriptionId, predicates)
136 then: 'the subscription cache handler is called once'
137 1 * mockDmiCacheHandler.add('test-id', predicates)
138 and: 'the mapper handler to get DMI in event is called once'
139 1 * mockDmiInEventMapper.toDmiInEvent(_)
140 and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
141 testDmiSubscriptionsPerDmi.size() * mockDmiInEventProducer.publishDmiInEvent(
142 'test-id', 'dmi-1', 'subscriptionDeleteRequest', _)
143 and: 'we schedule to send the response after configured time from the cache'
144 1 * mockNcmpOutEventProducer.publishNcmpOutEvent('test-id', 'subscriptionDeleteResponse', null, true)