d7b43aaca58c431ea9e2b0c887ec2b8fe60a7b63
[cps/ncmp-dmi-plugin.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.avcsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.core.builder.CloudEventBuilder
25 import org.onap.cps.ncmp.dmi.TestUtils
26 import org.onap.cps.ncmp.dmi.exception.CloudEventConstructionException
27 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.Data
28 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse
29 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionStatus
30 import org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.CmHandle
31 import org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent
32 import org.spockframework.spring.SpringBean
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.boot.test.context.SpringBootTest
35 import spock.lang.Specification
36
37 @SpringBootTest(classes = [ObjectMapper])
38 class SubscriptionEventResponseMapperSpec extends Specification {
39
40     @Autowired
41     def objectMapper = new ObjectMapper()
42
43     @SpringBean
44     SubscriptionEventResponseMapper objectUnderTest = new SubscriptionEventResponseMapper()
45
46     def 'Convert a SubscriptionResponseEvent to CloudEvent successfully.'() {
47         given: 'a SubscriptionResponseEvent and an event key'
48             def dmiName = 'test-ncmp-dmi'
49             def responseStatus = SubscriptionStatus.Status.ACCEPTED
50             def subscriptionStatuses = [new SubscriptionStatus(id: 'CmHandle1', status: responseStatus),
51                                         new SubscriptionStatus(id: 'CmHandle2', status: responseStatus)]
52             def subscriptionEventResponseData = new Data(subscriptionName: 'cm-subscription-001',
53                     clientId: 'SCO-9989752', dmiName: 'ncmp-dmi-plugin', subscriptionStatus: subscriptionStatuses)
54             def subscriptionEventResponse =
55                     new SubscriptionEventResponse().withData(subscriptionEventResponseData)
56         when: 'a SubscriptionResponseEvent is converted'
57             def result = objectUnderTest.toCloudEvent(subscriptionEventResponse,"subscriptionCreatedStatus", dmiName)
58         then: 'SubscriptionResponseEvent is converted as expected'
59             def expectedCloudEvent = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSource(URI.create('test-ncmp-dmi'))
60                 .withType("subscriptionCreated")
61                 .withDataSchema(URI.create("urn:cps:" + SubscriptionEventResponse.class.getName() + ":1.0.0"))
62                 .withExtension("correlationid", subscriptionEventResponse.getData().getClientId() + ":"
63                         + subscriptionEventResponse.getData().getSubscriptionName())
64                 .withData(objectMapper.writeValueAsBytes(subscriptionEventResponse)).build()
65             assert expectedCloudEvent.data == result.data
66     }
67
68     def 'Map the Cloud Event to data of the subscription event with incorrect content causes an exception'() {
69         given: 'an empty subscription response event and event key'
70             def dmiName = 'test-ncmp-dmi'
71             def testSubscriptionEventResponse = new SubscriptionEventResponse()
72         when: 'the subscription response event map to data of cloud event'
73             objectUnderTest.toCloudEvent(testSubscriptionEventResponse, "subscriptionCreatedStatus", dmiName)
74         then: 'a run time exception is thrown'
75            thrown(CloudEventConstructionException)
76     }
77
78     def 'Convert a CloudEvent to SubscriptionEvent.'() {
79         given: 'a CloudEvent'
80             def eventKey = UUID.randomUUID().toString()
81             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
82             def subscriptionEvent = objectMapper.readValue(jsonData, SubscriptionEvent.class)
83             def cloudEvent = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSource(URI.create('test-ncmp-dmi'))
84                     .withType("subscriptionCreated")
85                     .withDataSchema(URI.create("urn:cps:" + SubscriptionEvent.class.getName() + ":1.0.0"))
86                     .withExtension("correlationid", eventKey)
87                     .withData(objectMapper.writeValueAsBytes(subscriptionEvent)).build()
88         when: 'a SubscriptionEvent is formed'
89             def result = objectUnderTest.toSubscriptionEvent(cloudEvent)
90         then: 'Confirm SubscriptionEvent was formed as expected'
91             assert result == subscriptionEvent
92     }
93
94     def 'Convert a CloudEvent with Null data to SubscriptionEvent.'() {
95         given: 'a CloudEvent with null data'
96             def eventKey = UUID.randomUUID().toString()
97             def cloudEvent = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSource(URI.create('test-ncmp-dmi'))
98                     .withType("subscriptionCreated")
99                     .withDataSchema(URI.create("urn:cps:" + SubscriptionEvent.class.getName() + ":1.0.0"))
100                     .withExtension("correlationid", eventKey)
101                     .withData(null).build()
102         when: 'a SubscriptionEvent is formed'
103             def result = objectUnderTest.toSubscriptionEvent(cloudEvent)
104         then: 'Confirm SubscriptionEventResponse was formed as expected'
105             assert result == null
106     }
107 }