Merge "CM Subscription: Link method to publish to DMI in service layer"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / service / CmNotificationSubscriptionHandlerServiceImplSpec.groovy
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.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
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     def testListOfDeltaPredicates = []
54
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     }
76
77     def 'Consume valid and but non-unique CmNotificationSubscription create message'() {
78         given: 'a cmNotificationSubscriptionNcmp in event'
79             def jsonData = TestUtils.getResourceFileContent('cmSubscription/cmNotificationSubscriptionNcmpInEvent.json')
80             def testEventConsumed = jsonObjectMapper.convertJsonString(jsonData, CmNotificationSubscriptionNcmpInEvent.class)
81             mockCmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId('test-id') >> false
82         and: 'the NCMP out in event mapper returns an event for rejected request'
83             def testNcmpOutEvent = new CmNotificationSubscriptionNcmpOutEvent()
84             1 * mockCmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionNcmpOutEventForRejectedRequest(
85                 "test-id",_) >> testNcmpOutEvent
86         when: 'the valid but non-unique event is consumed'
87             objectUnderTest.processSubscriptionCreateRequest(testEventConsumed)
88         then: 'the events handler method to publish DMI event is never called'
89             0 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(_,_,_,_)
90         and: 'the events handler method to publish NCMP out event is called once'
91             1 * mockCmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(
92                 'test-id', 'subscriptionCreateResponse', testNcmpOutEvent, false)
93     }
94 }