ae6174964459cb5fbc8c247fff0522726b0704ee
[cps.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.api.impl.utils
22
23 import com.fasterxml.jackson.core.JsonProcessingException
24 import com.fasterxml.jackson.databind.ObjectMapper
25 import io.cloudevents.core.builder.CloudEventBuilder
26 import org.onap.cps.ncmp.events.avcsubscription1_0_0.client_to_ncmp.SubscriptionEvent
27 import org.onap.cps.ncmp.utils.TestUtils
28 import org.onap.cps.utils.JsonObjectMapper
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, JsonObjectMapper])
34 class SubscriptionEventCloudMapperSpec extends Specification {
35
36     @Autowired
37     JsonObjectMapper jsonObjectMapper
38
39     @Autowired
40     ObjectMapper objectMapper
41
42     def spyObjectMapper = Spy(ObjectMapper)
43
44     def objectUnderTest = new SubscriptionEventCloudMapper(spyObjectMapper)
45
46     def 'Map the data of the cloud event to subscription event'() {
47         given: 'a cloud event having a subscription event in the data part'
48             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
49             def testEventData = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
50             def testCloudEvent = CloudEventBuilder.v1()
51                 .withData(objectMapper.writeValueAsBytes(testEventData))
52                 .withId('some-event-id')
53                 .withType('subscriptionCreated')
54                 .withSource(URI.create('some-resource'))
55                 .withExtension('correlationid', 'test-cmhandle1').build()
56         when: 'the cloud event map to subscription event'
57             def resultSubscriptionEvent = objectUnderTest.toSubscriptionEvent(testCloudEvent)
58         then: 'the subscription event resulted having expected values'
59             resultSubscriptionEvent.getData() == testEventData.getData()
60     }
61
62     def 'Map the null of the data of the cloud event to subscription event'() {
63         given: 'a cloud event having a null subscription event in the data part'
64             def testCloudEvent = CloudEventBuilder.v1()
65                 .withData(null)
66                 .withId('some-event-id')
67                 .withType('subscriptionCreated')
68                 .withSource(URI.create('some-resource'))
69                 .withExtension('correlationid', 'test-cmhandle1').build()
70         when: 'the cloud event map to subscription event'
71             def resultSubscriptionEvent = objectUnderTest.toSubscriptionEvent(testCloudEvent)
72         then: 'the subscription event resulted having a null value'
73             resultSubscriptionEvent == null
74     }
75
76     def 'Map the subscription event to data of the cloud event'() {
77         given: 'a subscription event'
78             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEventNcmpVersion.json')
79             def testEventData = jsonObjectMapper.convertJsonString(jsonData,
80                                 org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent.class)
81             def testCloudEvent = CloudEventBuilder.v1()
82                 .withData(objectMapper.writeValueAsBytes(testEventData))
83                 .withId('some-id')
84                 .withType('subscriptionCreated')
85                 .withSource(URI.create('SCO-9989752'))
86                 .withExtension('correlationid', 'test-cmhandle1').build()
87         when: 'the subscription event map to data of cloud event'
88             SubscriptionEventCloudMapper.randomId = 'some-id'
89             def resultCloudEvent = objectUnderTest.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreated')
90         then: 'the subscription event resulted having expected values'
91             resultCloudEvent.getData() == testCloudEvent.getData()
92             resultCloudEvent.getId() == testCloudEvent.getId()
93             resultCloudEvent.getType() == testCloudEvent.getType()
94             resultCloudEvent.getSource() == URI.create('SCO-9989752')
95             resultCloudEvent.getDataSchema() == URI.create('urn:cps:org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent:1.0.0')
96     }
97
98     def 'Map the subscription event to cloud event with JSON processing exception'() {
99         given: 'a json processing exception during process'
100             def jsonProcessingException = new JsonProcessingException('The Cloud Event could not be constructed')
101             spyObjectMapper.writeValueAsBytes(_) >> { throw jsonProcessingException }
102         and: 'a subscription event of ncmp version'
103             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEventNcmpVersion.json')
104             def testEventData = jsonObjectMapper.convertJsonString(jsonData,
105                 org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent.class)
106         when: 'the subscription event map to cloud event'
107             def expectedResult = objectUnderTest.toCloudEvent(testEventData, 'some-key', 'some-event-type')
108         then: 'no exception is thrown since it has been handled already'
109             noExceptionThrown()
110         and: 'expected result should be null'
111             expectedResult == null
112     }
113
114 }