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