Fix: Make bookstore data consistent
[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('CREATE')
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('CREATE')
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-event-key')
79                 .withType('CREATE')
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()
88     }
89
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
95             try {
96                 SubscriptionEventCloudMapper.toCloudEvent(testNcmpSubscriptionEvent, 'some-key')
97             } catch (Exception e) {
98                 thrownException  = e
99             }
100         then: 'a run time exception is thrown'
101             assert thrownException instanceof RuntimeException
102     }
103
104 }