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.api.impl.events.cmsubscription.service
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.mapper.CmNotificationSubscriptionDmiInEventMapper
29 import org.onap.cps.ncmp.api.impl.events.cmsubscription.mapper.CmNotificationSubscriptionNcmpOutEventMapper
30 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.CmNotificationSubscriptionStatus
31 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails
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
39 class CmNotificationSubscriptionHandlerServiceImplSpec extends Specification{
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);
48 def objectUnderTest = new CmNotificationSubscriptionHandlerServiceImpl(mockCmNotificationSubscriptionPersistenceService,
49 mockCmNotificationSubscriptionDelta, mockCmNotificationSubscriptionMappersHandler,
50 mockCmNotificationSubscriptionEventsHandler, mockDmiCmNotificationSubscriptionCacheHandler)
52 def testSubscriptionDetailsMap = ["dmi-1":new DmiCmNotificationSubscriptionDetails([], CmNotificationSubscriptionStatus.PENDING)]
53 def testListOfDeltaPredicates = []
55 def 'Consume valid and unique CmNotificationSubscriptionNcmpInEvent create message'() {
56 given: 'a cmNotificationSubscriptionNcmp in event with unique subscription id'
57 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
58 def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
59 mockCmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId("test-id") >> true
60 and: 'the cache handler returns for relevant subscription id'
61 1 * mockDmiCmNotificationSubscriptionCacheHandler.get("test-id") >> testSubscriptionDetailsMap
62 and: 'the delta predicates is returned'
63 1 * mockCmNotificationSubscriptionDelta.getDelta(_) >> testListOfDeltaPredicates
64 and: 'the DMI in event mapper returns cm notification subscription event'
65 def testDmiInEvent = new CmNotificationSubscriptionDmiInEvent()
66 1 * mockCmNotificationSubscriptionMappersHandler
67 .toCmNotificationSubscriptionDmiInEvent(testListOfDeltaPredicates) >> testDmiInEvent
68 when: 'the valid and unique event is consumed'
69 objectUnderTest.processSubscriptionCreateRequest(testEventConsumed)
70 then: 'the subscription cache handler is called once'
71 1 * mockDmiCmNotificationSubscriptionCacheHandler.add('test-id',_)
72 and: 'the events handler method to publish DMI event is called correct number of times with the correct parameters'
73 testSubscriptionDetailsMap.size() * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(
74 "test-id", "dmi-1", "subscriptionCreateRequest", testDmiInEvent)
75 and: 'we schedule to send the response after configured time from the cache'
76 1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(
77 "test-id", "subscriptionCreateResponse", null, true)
80 def 'Consume valid and but non-unique CmNotificationSubscription create message'() {
81 given: 'a cmNotificationSubscriptionNcmp in event'
82 def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
83 def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
84 mockCmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId('test-id') >> false
85 and: 'the NCMP out in event mapper returns an event for rejected request'
86 def testNcmpOutEvent = new CmNotificationSubscriptionNcmpOutEvent()
87 1 * mockCmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionNcmpOutEventForRejectedRequest(
88 "test-id",_) >> testNcmpOutEvent
89 when: 'the valid but non-unique event is consumed'
90 objectUnderTest.processSubscriptionCreateRequest(testEventConsumed)
91 then: 'the events handler method to publish DMI event is never called'
92 0 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(_,_,_,_)
93 and: 'the events handler method to publish NCMP out event is called once'
94 1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(
95 'test-id', 'subscriptionCreateResponse', testNcmpOutEvent, false)