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.spi.exceptions.CloudEventConstructionException
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 'Map the data of the cloud event to subscription event'() {
43         given: 'a cloud event having a subscription event in the data part'
44             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
45             def testEventData = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEvent.class)
46             def testCloudEvent = CloudEventBuilder.v1()
47                 .withData(objectMapper.writeValueAsBytes(testEventData))
48                 .withId('some-event-id')
49                 .withType('subscriptionCreated')
50                 .withSource(URI.create('some-resource'))
51                 .withExtension('correlationid', 'test-cmhandle1').build()
52         when: 'the cloud event map to subscription event'
53             def resultSubscriptionEvent = SubscriptionEventCloudMapper.toSubscriptionEvent(testCloudEvent)
54         then: 'the subscription event resulted having expected values'
55             resultSubscriptionEvent.getData() == testEventData.getData()
56     }
57
58     def 'Map the null of the data of the cloud event to subscription event'() {
59         given: 'a cloud event having a null subscription event in the data part'
60             def testCloudEvent = CloudEventBuilder.v1()
61                 .withData(null)
62                 .withId('some-event-id')
63                 .withType('subscriptionCreated')
64                 .withSource(URI.create('some-resource'))
65                 .withExtension('correlationid', 'test-cmhandle1').build()
66         when: 'the cloud event map to subscription event'
67             def resultSubscriptionEvent = SubscriptionEventCloudMapper.toSubscriptionEvent(testCloudEvent)
68         then: 'the subscription event resulted having a null value'
69             resultSubscriptionEvent == null
70     }
71
72     def 'Map the subscription event to data of the cloud event'() {
73         given: 'a subscription event'
74             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEventNcmpVersion.json')
75             def testEventData = jsonObjectMapper.convertJsonString(jsonData,
76                                 org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent.class)
77             def testCloudEvent = CloudEventBuilder.v1()
78                 .withData(objectMapper.writeValueAsBytes(testEventData))
79                 .withId('some-id')
80                 .withType('subscriptionCreated')
81                 .withSource(URI.create('SCO-9989752'))
82                 .withExtension('correlationid', 'test-cmhandle1').build()
83         when: 'the subscription event map to data of cloud event'
84             SubscriptionEventCloudMapper.randomId = 'some-id'
85             def resultCloudEvent = SubscriptionEventCloudMapper.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreated')
86         then: 'the subscription event resulted having expected values'
87             resultCloudEvent.getData() == testCloudEvent.getData()
88             resultCloudEvent.getId() == testCloudEvent.getId()
89             resultCloudEvent.getType() == testCloudEvent.getType()
90             resultCloudEvent.getSource() == URI.create('SCO-9989752')
91             resultCloudEvent.getDataSchema() == URI.create('urn:cps:org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent:1.0.0')
92     }
93
94     def 'Map the subscription event to data of the cloud event with wrong content causes an exception'() {
95         given: 'an empty ncmp subscription event'
96             def testNcmpSubscriptionEvent = new org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent()
97         when: 'the subscription event map to data of cloud event'
98             SubscriptionEventCloudMapper.toCloudEvent(testNcmpSubscriptionEvent, 'some-key', 'some-event-type')
99         then: 'a run time exception is thrown'
100             def exception = thrown(CloudEventConstructionException)
101             exception.details == 'Invalid object to serialize or required headers is missing'
102     }
103
104 }