89b13e26e59d152e0a47c4261ef2ef5b712f145d
[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.databind.ObjectMapper
24 import io.cloudevents.core.builder.CloudEventBuilder
25 import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent
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 SubscriptionEventResponseCloudMapperSpec extends Specification {
34
35     @Autowired
36     JsonObjectMapper jsonObjectMapper
37
38     @Autowired
39     ObjectMapper objectMapper
40
41     def spyObjectMapper = Spy(ObjectMapper)
42
43     def objectUnderTest = new SubscriptionEventResponseCloudMapper(spyObjectMapper)
44
45     def 'Map the cloud event to subscription event response'() {
46         given: 'a cloud event having a subscription event response in the data part'
47             def jsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
48             def testEventData = jsonObjectMapper.convertJsonString(jsonData, CmSubscriptionDmiOutEvent.class)
49             def testCloudEvent = CloudEventBuilder.v1()
50                 .withData(objectMapper.writeValueAsBytes(testEventData))
51                 .withId('some-event-id')
52                 .withType('subscriptionCreatedStatus')
53                 .withSource(URI.create('some-resource'))
54                 .withExtension('correlationid', 'test-cmhandle1').build()
55         when: 'the cloud event map to subscription event response'
56             def resultSubscriptionEvent = objectUnderTest.toCmSubscriptionDmiOutEvent(testCloudEvent)
57         then: 'the subscription event resulted having expected values'
58             resultSubscriptionEvent.getData() == testEventData.getData()
59     }
60
61     def 'Map null of the data of the cloud event to subscription event response'() {
62         given: 'a cloud event having a null subscription event response in the data part'
63             def testCloudEvent = CloudEventBuilder.v1()
64                 .withData(null)
65                 .withId('some-event-id')
66                 .withType('subscriptionCreatedStatus')
67                 .withSource(URI.create('some-resource'))
68                 .withExtension('correlationid', 'test-cmhandle1').build()
69         when: 'the cloud event map to subscription event response'
70             def resultSubscriptionEvent = objectUnderTest.toCmSubscriptionDmiOutEvent(testCloudEvent)
71         then: 'the subscription event response resulted having a null value'
72             resultSubscriptionEvent == null
73     }
74 }