Merge "Use constants for magic numbers in perf tests"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / utils / SubscriptionEventCloudMapperSpec.groovy
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.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
31
32 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
33 class SubscriptionEventCloudMapperSpec extends Specification {
34
35     @Autowired
36     JsonObjectMapper jsonObjectMapper
37
38     @Autowired
39     ObjectMapper objectMapper
40
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')
48                 .withType('subscriptionCreated')
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()
55     }
56
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()
60                 .withData(null)
61                 .withId('some-event-id')
62                 .withType('subscriptionCreated')
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
69     }
70
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-id')
79                 .withType('subscriptionCreated')
80                 .withSource(URI.create('SCO-9989752'))
81                 .withExtension('correlationid', 'test-cmhandle1').build()
82         when: 'the subscription event map to data of cloud event'
83             SubscriptionEventCloudMapper.randomId = 'some-id'
84             def resultCloudEvent = SubscriptionEventCloudMapper.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreated')
85         then: 'the subscription event resulted having expected values'
86             resultCloudEvent.getData() == testCloudEvent.getData()
87             resultCloudEvent.getId() == testCloudEvent.getId()
88             resultCloudEvent.getType() == testCloudEvent.getType()
89             resultCloudEvent.getSource() == URI.create('SCO-9989752')
90             resultCloudEvent.getDataSchema() == URI.create('urn:cps:org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent:1.0.0')
91     }
92
93     def 'Map the subscription event to data of the cloud event with wrong content causes an exception'() {
94         given: 'an empty ncmp subscription event'
95             def testNcmpSubscriptionEvent = new org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent()
96         when: 'the subscription event map to data of cloud event'
97             SubscriptionEventCloudMapper.toCloudEvent(testNcmpSubscriptionEvent, 'some-key', 'some-event-type')
98         then: 'a run time exception is thrown'
99             def exception = thrown(CloudEventConstructionException)
100             exception.details == 'Invalid object to serialize or required headers is missing'
101     }
102
103 }