Set CPS Project Status to 'Mature'
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / CmNotificationSubscriptionDmiInEventProducerSpec.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
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import org.onap.cps.events.EventsPublisher
26 import org.onap.cps.ncmp.api.impl.events.cmsubscription.producer.CmNotificationSubscriptionDmiInEventProducer
27 import org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper
28 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.ncmp_to_dmi.CmNotificationSubscriptionDmiInEvent
29 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.ncmp_to_dmi.Cmhandle
30 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.ncmp_to_dmi.Data
31 import org.onap.cps.utils.JsonObjectMapper
32 import spock.lang.Specification
33
34 class CmNotificationSubscriptionDmiInEventProducerSpec extends Specification {
35
36     def mockEventsPublisher = Mock(EventsPublisher)
37     def jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
38
39     def objectUnderTest = new CmNotificationSubscriptionDmiInEventProducer(mockEventsPublisher, jsonObjectMapper)
40
41     def 'Create and Publish Cm Notification Subscription DMI In Event'() {
42         given: 'a cm subscription for a dmi plugin'
43             def subscriptionId = 'test-subscription-id'
44             def dmiPluginName = 'test-dmiplugin'
45             def eventType = 'subscriptionCreateRequest'
46             def cmNotificationSubscriptionDmiInEvent = new CmNotificationSubscriptionDmiInEvent(data: new Data(cmhandles: [new Cmhandle(cmhandleId: 'test-1', privateProperties: [:])]))
47         and: 'also we have target topic for dmiPlugin'
48             objectUnderTest.cmNotificationSubscriptionDmiInEventTopic = 'dmiplugin-test-topic'
49         when: 'the event is published'
50             objectUnderTest.publishCmNotificationSubscriptionDmiInEvent(subscriptionId, dmiPluginName, eventType, cmNotificationSubscriptionDmiInEvent)
51         then: 'the event contains the required attributes'
52             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
53                 args ->
54                     {
55                         assert args[0] == 'dmiplugin-test-topic'
56                         assert args[1] == subscriptionId
57                         def cmNotificationSubscriptionDmiInEventAsCloudEvent = (args[2] as CloudEvent)
58                         assert cmNotificationSubscriptionDmiInEventAsCloudEvent.getExtension('correlationid') == subscriptionId + '#' + dmiPluginName
59                         assert cmNotificationSubscriptionDmiInEventAsCloudEvent.type == 'subscriptionCreateRequest'
60                         assert cmNotificationSubscriptionDmiInEventAsCloudEvent.source.toString() == 'NCMP'
61                         assert CloudEventMapper.toTargetEvent(cmNotificationSubscriptionDmiInEventAsCloudEvent, CmNotificationSubscriptionDmiInEvent) == cmNotificationSubscriptionDmiInEvent
62                     }
63             }
64     }
65 }