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