49e43f9067522027e06d4b526ec7d3fdc8fe27f4
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2024-2025 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.impl.cmnotificationsubscription.dmi
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.core.v1.CloudEventBuilder
26 import org.onap.cps.events.EventsPublisher
27 import org.onap.cps.ncmp.config.CpsApplicationContext
28 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_dmi.CmHandle
29 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_dmi.Data
30 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_dmi.DmiInEvent
31 import org.onap.cps.ncmp.utils.events.CloudEventMapper
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.test.context.ContextConfiguration
35 import spock.lang.Specification
36
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, CloudEventBuilder])
38 @ContextConfiguration(classes = [CpsApplicationContext])
39 class DmiInEventProducerSpec extends Specification {
40
41     def mockEventsPublisher = Mock(EventsPublisher)
42
43     def objectUnderTest = new DmiInEventProducer(mockEventsPublisher)
44
45     def 'Create and Publish Cm Notification Subscription DMI In Event'() {
46         given: 'a cm subscription for a dmi plugin'
47             def subscriptionId = 'test-subscription-id'
48             def dmiPluginName = 'test-dmiplugin'
49             def eventType = 'subscriptionCreateRequest'
50             def dmiInEvent = new DmiInEvent(data: new Data(cmHandles: [new CmHandle(cmhandleId: 'test-1', privateProperties: [:])]))
51         and: 'also we have target topic for dmiPlugin'
52             objectUnderTest.dmiInEventTopic = 'dmiplugin-test-topic'
53         when: 'the event is published'
54             objectUnderTest.publishDmiInEvent(subscriptionId, dmiPluginName, eventType, dmiInEvent)
55         then: 'the event contains the required attributes'
56             1 * mockEventsPublisher.publishCloudEvent(_, _, _) >> {
57                 args ->
58                     {
59                         assert args[0] == 'dmiplugin-test-topic'
60                         assert args[1] == subscriptionId
61                         def dmiInEventAsCloudEvent = (args[2] as CloudEvent)
62                         assert dmiInEventAsCloudEvent.getExtension('correlationid') == subscriptionId + '#' + dmiPluginName
63                         assert dmiInEventAsCloudEvent.type == 'subscriptionCreateRequest'
64                         assert dmiInEventAsCloudEvent.source.toString() == 'NCMP'
65                         assert CloudEventMapper.toTargetEvent(dmiInEventAsCloudEvent, DmiInEvent) == dmiInEvent
66                     }
67             }
68     }
69 }