Create module structure in DMI Plugin
[cps/ncmp-dmi-plugin.git] / dmi-service / src / test / groovy / org / onap / cps / ncmp / dmi / notifications / cmsubscription / CmNotificationSubscriptionDmiOutEventToCloudEventMapperSpec.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.dmi.notifications.cmsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.core.builder.CloudEventBuilder
25 import org.onap.cps.ncmp.dmi.exception.CloudEventConstructionException
26 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.dmi_to_ncmp.CmNotificationSubscriptionDmiOutEvent
27 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.dmi_to_ncmp.Data
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.test.context.SpringBootTest
31 import spock.lang.Specification
32
33 @SpringBootTest(classes = [ObjectMapper])
34 class CmNotificationSubscriptionDmiOutEventToCloudEventMapperSpec extends Specification {
35
36     @Autowired
37     def objectMapper = new ObjectMapper()
38
39     @SpringBean
40     CmNotificationSubscriptionDmiOutEventToCloudEventMapper objectUnderTest = new CmNotificationSubscriptionDmiOutEventToCloudEventMapper()
41
42     def 'Convert a Cm Subscription DMI Out Event to CloudEvent successfully.'() {
43         given: 'a Cm Subscription DMI Out Event and an event key'
44             def dmiName = 'test-ncmp-dmi'
45             def correlationId = 'subscription1#test-ncmp-dmi'
46             def cmSubscriptionDmiOutEventData = new Data(statusCode: "1", statusMessage: "accepted")
47             def cmSubscriptionDmiOutEvent =
48                     new CmNotificationSubscriptionDmiOutEvent().withData(cmSubscriptionDmiOutEventData)
49         when: 'a Cm Subscription DMI Out Event is converted'
50             def result = objectUnderTest.toCloudEvent(cmSubscriptionDmiOutEvent, "subscriptionCreatedStatus", dmiName, correlationId)
51         then: 'Cm Subscription DMI Out Event is converted as expected'
52             def expectedCloudEvent = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSource(URI.create('test-ncmp-dmi'))
53                     .withType("subscriptionCreated")
54                     .withDataSchema(URI.create("urn:cps:" + CmNotificationSubscriptionDmiOutEvent.class.getName() + ":1.0.0"))
55                     .withExtension("correlationid", correlationId)
56                     .withData(objectMapper.writeValueAsBytes(cmSubscriptionDmiOutEvent)).build()
57             assert expectedCloudEvent.data == result.data
58     }
59
60     def 'Map the Cloud Event to data of the subscription event with null parameters causes an exception'() {
61         given: 'an empty subscription response event and event key'
62             def correlationId = 'subscription1#test-ncmp-dmi'
63             def cmSubscriptionDmiOutEvent = new CmNotificationSubscriptionDmiOutEvent()
64         when: 'the cm subscription dmi out Event map to data of cloud event'
65             objectUnderTest.toCloudEvent(cmSubscriptionDmiOutEvent, "subscriptionCreatedStatus", null , correlationId)
66         then: 'a run time exception is thrown'
67             thrown(CloudEventConstructionException)
68     }
69 }