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
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.api.impl.utils
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.core.builder.CloudEventBuilder
25 import org.onap.cps.ncmp.events.avcsubscription1_0_0.client_to_ncmp.SubscriptionEvent
26 import org.onap.cps.ncmp.utils.TestUtils
27 import org.onap.cps.utils.JsonObjectMapper
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.test.context.SpringBootTest
30 import spock.lang.Specification
32 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
33 class SubscriptionEventCloudMapperSpec extends Specification {
36 JsonObjectMapper jsonObjectMapper
39 ObjectMapper objectMapper
41 def 'Map the data of the cloud event to subscription event'() {
42 given: 'a cloud event having a subscription event in the data part'
43 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
44 def testEventData = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
45 def testCloudEvent = CloudEventBuilder.v1()
46 .withData(objectMapper.writeValueAsBytes(testEventData))
47 .withId('some-event-id')
49 .withSource(URI.create('some-resource'))
50 .withExtension('correlationid', 'test-cmhandle1').build()
51 when: 'the cloud event map to subscription event'
52 def resultSubscriptionEvent = SubscriptionEventCloudMapper.toSubscriptionEvent(testCloudEvent)
53 then: 'the subscription event resulted having expected values'
54 resultSubscriptionEvent.getData() == testEventData.getData()
57 def 'Map the null of the data of the cloud event to subscription event'() {
58 given: 'a cloud event having a null subscription event in the data part'
59 def testCloudEvent = CloudEventBuilder.v1()
61 .withId('some-event-id')
63 .withSource(URI.create('some-resource'))
64 .withExtension('correlationid', 'test-cmhandle1').build()
65 when: 'the cloud event map to subscription event'
66 def resultSubscriptionEvent = SubscriptionEventCloudMapper.toSubscriptionEvent(testCloudEvent)
67 then: 'the subscription event resulted having a null value'
68 resultSubscriptionEvent == null
71 def 'Map the subscription event to data of the cloud event'() {
72 given: 'a subscription event'
73 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEventNcmpVersion.json')
74 def testEventData = jsonObjectMapper.convertJsonString(jsonData,
75 org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent.class)
76 def testCloudEvent = CloudEventBuilder.v1()
77 .withData(objectMapper.writeValueAsBytes(testEventData))
78 .withId('some-event-key')
80 .withSource(URI.create('some-resource'))
81 .withExtension('correlationid', 'test-cmhandle1').build()
82 when: 'the subscription event map to data of cloud event'
83 def resultCloudEvent = SubscriptionEventCloudMapper.toCloudEvent(testEventData, 'some-event-key')
84 then: 'the subscription event resulted having expected values'
85 resultCloudEvent.getData() == testCloudEvent.getData()
86 resultCloudEvent.getId() == testCloudEvent.getId()
87 resultCloudEvent.getType() == testCloudEvent.getType()
90 def 'Map the subscription event to data of the cloud event with wrong content causes an exception'() {
91 given: 'an empty ncmp subscription event'
92 def testNcmpSubscriptionEvent = new org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent()
93 when: 'the subscription event map to data of cloud event'
94 def thrownException = null
96 SubscriptionEventCloudMapper.toCloudEvent(testNcmpSubscriptionEvent, 'some-key')
97 } catch (Exception e) {
100 then: 'a run time exception is thrown'
101 assert thrownException instanceof RuntimeException