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
 
   9  *        http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  17  *  SPDX-License-Identifier: Apache-2.0
 
  18  *  ============LICENSE_END=========================================================
 
  21 package org.onap.cps.ncmp.api.impl.util.deprecated
 
  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.api.impl.utils.CmSubscriptionEventCloudMapper
 
  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
 
  34 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
 
  35 class CmSubscriptionEventCloudMapperSpec extends Specification {
 
  38     JsonObjectMapper jsonObjectMapper
 
  41     ObjectMapper objectMapper
 
  43     def spyObjectMapper = Spy(ObjectMapper)
 
  45     def objectUnderTest = new CmSubscriptionEventCloudMapper(spyObjectMapper)
 
  47     def 'Map the subscription event to data of the cloud event'() {
 
  48         given: 'a subscription event'
 
  49             def jsonData = TestUtils.getResourceFileContent('deprecatedCmSubscription/cmSubscriptionDmiInEvent.json')
 
  50             def testEventData = jsonObjectMapper.convertJsonString(jsonData,
 
  51                 CmSubscriptionDmiInEvent.class)
 
  52             def testCloudEvent = CloudEventBuilder.v1()
 
  53                 .withData(objectMapper.writeValueAsBytes(testEventData))
 
  55                 .withType('subscriptionCreated')
 
  56                 .withSource(URI.create('SCO-9989752'))
 
  57                 .withExtension('correlationid', 'test-cmhandle1').build()
 
  58         when: 'the subscription event map to data of cloud event'
 
  59             CmSubscriptionEventCloudMapper.randomId = 'some-id'
 
  60             def resultCloudEvent = objectUnderTest.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreated')
 
  61         then: 'the subscription event resulted having expected values'
 
  62             resultCloudEvent.getData() == testCloudEvent.getData()
 
  63             resultCloudEvent.getId() == testCloudEvent.getId()
 
  64             resultCloudEvent.getType() == testCloudEvent.getType()
 
  65             resultCloudEvent.getSource() == URI.create('SCO-9989752')
 
  66             resultCloudEvent.getDataSchema() == URI.create('urn:cps:org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_dmi.CmSubscriptionDmiInEvent:1.0.0')
 
  69     def 'Map the subscription event to cloud event with JSON processing exception'() {
 
  70         given: 'a json processing exception during process'
 
  71             def jsonProcessingException = new JsonProcessingException('The Cloud Event could not be constructed')
 
  72             spyObjectMapper.writeValueAsBytes(_) >> { throw jsonProcessingException }
 
  73         and: 'a subscription event of ncmp version'
 
  74             def jsonData = TestUtils.getResourceFileContent('deprecatedCmSubscription/cmSubscriptionDmiInEvent.json')
 
  75             def testEventData = jsonObjectMapper.convertJsonString(jsonData,
 
  76                 CmSubscriptionDmiInEvent.class)
 
  77         when: 'the subscription event map to cloud event'
 
  78             def expectedResult = objectUnderTest.toCloudEvent(testEventData, 'some-key', 'some-event-type')
 
  79         then: 'no exception is thrown since it has been handled already'
 
  81         and: 'expected result should be null'
 
  82             expectedResult == null