Bump CPS to 3.4.2-SNAPSHOT
[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.ncmp_to_dmi.CmSubscriptionDmiInEvent
27 import org.onap.cps.ncmp.utils.TestUtils
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 CmSubscriptionEventCloudMapperSpec extends Specification {
35
36     @Autowired
37     JsonObjectMapper jsonObjectMapper
38
39     @Autowired
40     ObjectMapper objectMapper
41
42     def spyObjectMapper = Spy(ObjectMapper)
43
44     def objectUnderTest = new CmSubscriptionEventCloudMapper(spyObjectMapper)
45
46     def 'Map the subscription event to data of the cloud event'() {
47         given: 'a subscription event'
48             def jsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiInEvent.json')
49             def testEventData = jsonObjectMapper.convertJsonString(jsonData,
50                 CmSubscriptionDmiInEvent.class)
51             def testCloudEvent = CloudEventBuilder.v1()
52                 .withData(objectMapper.writeValueAsBytes(testEventData))
53                 .withId('some-id')
54                 .withType('subscriptionCreated')
55                 .withSource(URI.create('SCO-9989752'))
56                 .withExtension('correlationid', 'test-cmhandle1').build()
57         when: 'the subscription event map to data of cloud event'
58             CmSubscriptionEventCloudMapper.randomId = 'some-id'
59             def resultCloudEvent = objectUnderTest.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreated')
60         then: 'the subscription event resulted having expected values'
61             resultCloudEvent.getData() == testCloudEvent.getData()
62             resultCloudEvent.getId() == testCloudEvent.getId()
63             resultCloudEvent.getType() == testCloudEvent.getType()
64             resultCloudEvent.getSource() == URI.create('SCO-9989752')
65             resultCloudEvent.getDataSchema() == URI.create('urn:cps:org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_dmi.CmSubscriptionDmiInEvent:1.0.0')
66     }
67
68     def 'Map the subscription event to cloud event with JSON processing exception'() {
69         given: 'a json processing exception during process'
70             def jsonProcessingException = new JsonProcessingException('The Cloud Event could not be constructed')
71             spyObjectMapper.writeValueAsBytes(_) >> { throw jsonProcessingException }
72         and: 'a subscription event of ncmp version'
73             def jsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiInEvent.json')
74             def testEventData = jsonObjectMapper.convertJsonString(jsonData,
75                 CmSubscriptionDmiInEvent.class)
76         when: 'the subscription event map to cloud event'
77             def expectedResult = objectUnderTest.toCloudEvent(testEventData, 'some-key', 'some-event-type')
78         then: 'no exception is thrown since it has been handled already'
79             noExceptionThrown()
80         and: 'expected result should be null'
81             expectedResult == null
82     }
83
84 }