982150ec0a19d14ea0fa5b92f0bab48fad913811
[cps.git] /
1 /*
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
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.cmsubscription.service
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionDelta
25 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionEventsHandler
26 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionMappersHandler
27 import org.onap.cps.ncmp.api.impl.events.cmsubscription.DmiCmNotificationSubscriptionCacheHandler
28 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.CmNotificationSubscriptionStatus
29 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails
30 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionPredicate
31 import org.onap.cps.ncmp.api.impl.operations.DatastoreType
32 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.CmNotificationSubscriptionNcmpInEvent
33 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.ncmp_to_dmi.CmNotificationSubscriptionDmiInEvent
34 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent
35 import org.onap.cps.ncmp.utils.TestUtils
36 import org.onap.cps.utils.JsonObjectMapper
37 import spock.lang.Specification
38
39 class CmNotificationSubscriptionHandlerServiceImplSpec extends Specification{
40
41     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
42     def mockCmNotificationSubscriptionPersistenceService = Mock(CmNotificationSubscriptionPersistenceService);
43     def mockCmNotificationSubscriptionDelta = Mock(CmNotificationSubscriptionDelta);
44     def mockCmNotificationSubscriptionMappersHandler = Mock(CmNotificationSubscriptionMappersHandler);
45     def mockCmNotificationSubscriptionEventsHandler = Mock(CmNotificationSubscriptionEventsHandler);
46     def mockDmiCmNotificationSubscriptionCacheHandler = Mock(DmiCmNotificationSubscriptionCacheHandler);
47
48     def objectUnderTest = new CmNotificationSubscriptionHandlerServiceImpl(mockCmNotificationSubscriptionPersistenceService,
49         mockCmNotificationSubscriptionDelta, mockCmNotificationSubscriptionMappersHandler,
50         mockCmNotificationSubscriptionEventsHandler, mockDmiCmNotificationSubscriptionCacheHandler)
51
52     def testSubscriptionDetailsMap = ["dmi-1":new DmiCmNotificationSubscriptionDetails([], CmNotificationSubscriptionStatus.PENDING)]
53
54     def 'Consume valid and unique CmNotificationSubscriptionNcmpInEvent create message'() {
55         given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
56             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
57             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
58             def testListOfDeltaPredicates = [new DmiCmNotificationSubscriptionPredicate(['ch1'].toSet(), DatastoreType.PASSTHROUGH_OPERATIONAL, ['/a/b'].toSet())]
59             mockCmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
60         and: 'relevant details is extracted from the event'
61             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
62             def predicates = testEventConsumed.getData().getPredicates()
63         and: 'the cache handler returns for relevant subscription id'
64             1 * mockDmiCmNotificationSubscriptionCacheHandler.get("test-id") >> testSubscriptionDetailsMap
65         and: 'the delta predicates is returned'
66             1 * mockCmNotificationSubscriptionDelta.getDelta(_) >> testListOfDeltaPredicates
67         and: 'the DMI in event mapper returns cm notification subscription event'
68             def testDmiInEvent = new CmNotificationSubscriptionDmiInEvent()
69             1 *  mockCmNotificationSubscriptionMappersHandler
70                 .toCmNotificationSubscriptionDmiInEvent(testListOfDeltaPredicates) >> testDmiInEvent
71         when: 'the valid and unique event is consumed'
72             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
73         then: 'the subscription cache handler is called once'
74             1 * mockDmiCmNotificationSubscriptionCacheHandler.add('test-id',_)
75         and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
76             testSubscriptionDetailsMap.size() * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(
77                 "test-id", "dmi-1", "subscriptionCreateRequest", testDmiInEvent)
78         and: 'we schedule to send the response after configured time from the cache'
79             1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(
80                 "test-id", "subscriptionCreateResponse", null, true)
81     }
82
83     def 'Consume valid and Overlapping Cm Notification Subscription NcmpIn Event'() {
84         given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
85             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
86             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
87             def noDeltaPredicates = []
88             mockCmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
89         and: 'the cache handler returns for relevant subscription id'
90             1 * mockDmiCmNotificationSubscriptionCacheHandler.get("test-id") >> testSubscriptionDetailsMap
91         and: 'the delta predicates is returned'
92             1 * mockCmNotificationSubscriptionDelta.getDelta(_) >> noDeltaPredicates
93         when: 'the valid and unique event is consumed'
94             objectUnderTest.processSubscriptionCreateRequest('test-id', noDeltaPredicates)
95         then: 'the subscription cache handler is called once'
96             1 * mockDmiCmNotificationSubscriptionCacheHandler.add('test-id', _)
97         and: 'the subscription details are updated in the cache'
98             1 * mockDmiCmNotificationSubscriptionCacheHandler.updateDmiCmNotificationSubscriptionStatusPerDmi('test-id', _, CmNotificationSubscriptionStatus.ACCEPTED)
99         and: 'we schedule to send the response after configured time from the cache'
100             1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(
101                 "test-id", "subscriptionCreateResponse", null, true)
102     }
103
104     def 'Consume valid and but non-unique CmNotificationSubscription create message'() {
105         given: 'a cmNotificationSubscriptionNcmp in event'
106             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
107             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
108             mockCmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId('test-id') >> false
109         and: 'relevant details is extracted from the event'
110             def subscriptionId = testEventConsumed.getData().getSubscriptionId()
111             def predicates = testEventConsumed.getData().getPredicates()
112         and: 'the NCMP out in event mapper returns an event for rejected request'
113             def testNcmpOutEvent = new CmNotificationSubscriptionNcmpOutEvent()
114             1 * mockCmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionNcmpOutEventForRejectedRequest(
115                 "test-id",_) >> testNcmpOutEvent
116         when: 'the valid but non-unique event is consumed'
117             objectUnderTest.processSubscriptionCreateRequest(subscriptionId, predicates)
118         then: 'the events handler method to publish DMI event is never called'
119             0 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(_,_,_,_)
120         and: 'the events handler method to publish NCMP out event is called once'
121             1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(
122                 'test-id', 'subscriptionCreateResponse', testNcmpOutEvent, false)
123     }
124
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, CmNotificationSubscriptionNcmpInEvent.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 * mockDmiCmNotificationSubscriptionCacheHandler.get('test-id') >> testSubscriptionDetailsMap
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 * mockDmiCmNotificationSubscriptionCacheHandler.add('test-id', predicates)
138         and: 'the mapper handler to get DMI in event is called once'
139             1 * mockCmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionDmiInEvent(_)
140         and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
141             testSubscriptionDetailsMap.size() * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(
142                 'test-id', 'dmi-1', 'subscriptionDeleteRequest', _)
143         and: 'we schedule to send the response after configured time from the cache'
144             1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(
145                 'test-id', 'subscriptionDeleteResponse', null, true)
146     }
147 }