diff options
author | halil.cakal <halil.cakal@est.tech> | 2023-08-01 16:35:58 +0100 |
---|---|---|
committer | halil.cakal <halil.cakal@est.tech> | 2023-08-04 14:41:42 +0100 |
commit | 640e0ed06320d594e0ef18557f0051ead853712e (patch) | |
tree | 33c0989d420facf4c088e91776ae8489be6ec415 /cps-ncmp-service/src/test/groovy/org/onap | |
parent | 7a049060b9ee2306eb562f75a1cbd0d50eb14f41 (diff) |
Subscription Creation: Fixes for code review after demo
- Change missleading subscription details in testware
- Change datastore check to comply with CPS enum
- Remove redundant CloudConstructionException
- Change exception handling in cloud event mappers
to avoid loss of information
- Remove exception handling from scheduler service
as discussed in the meeting
Issue-ID: CPS-1732
Change-Id: I9fee2eafd4db97a0eed80e39219463c904f5a980
Signed-off-by: halil.cakal <halil.cakal@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/test/groovy/org/onap')
10 files changed, 201 insertions, 26 deletions
diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/ClientSubscriptionEventMapperSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/ClientSubscriptionEventMapperSpec.groovy index 85a58cdf30..b3d81e33c6 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/ClientSubscriptionEventMapperSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/ClientSubscriptionEventMapperSpec.groovy @@ -54,7 +54,7 @@ class ClientSubscriptionEventMapperSpec extends Specification { and: 'predicate targets is null' assert result.getData().getPredicates().getTargets() == [] and: 'datastore is passthrough-running' - assert result.getData().getPredicates().getDatastore() == 'passthrough-running' + assert result.getData().getPredicates().getDatastore() == 'ncmp-datastore:passthrough-running' } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventConsumerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventConsumerSpec.groovy index 7fa8155a2b..430d12207c 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventConsumerSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventConsumerSpec.groovy @@ -25,6 +25,7 @@ import io.cloudevents.CloudEvent import io.cloudevents.core.builder.CloudEventBuilder import org.apache.kafka.clients.consumer.ConsumerRecord import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence +import org.onap.cps.ncmp.api.impl.utils.SubscriptionEventCloudMapper import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec import org.onap.cps.ncmp.events.avcsubscription1_0_0.client_to_ncmp.SubscriptionEvent; @@ -39,7 +40,8 @@ class SubscriptionEventConsumerSpec extends MessagingBaseSpec { def mockSubscriptionEventForwarder = Mock(SubscriptionEventForwarder) def mockSubscriptionEventMapper = Mock(SubscriptionEventMapper) def mockSubscriptionPersistence = Mock(SubscriptionPersistence) - def objectUnderTest = new SubscriptionEventConsumer(mockSubscriptionEventForwarder, mockSubscriptionEventMapper, mockSubscriptionPersistence) + def subscriptionEventCloudMapper = new SubscriptionEventCloudMapper(new ObjectMapper()) + def objectUnderTest = new SubscriptionEventConsumer(mockSubscriptionEventForwarder, mockSubscriptionEventMapper, mockSubscriptionPersistence, subscriptionEventCloudMapper) def yangModelSubscriptionEvent = new YangModelSubscriptionEvent() diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventForwarderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventForwarderSpec.groovy index 4193f75545..dd93bf6f03 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventForwarderSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventForwarderSpec.groovy @@ -30,6 +30,7 @@ import org.mapstruct.factory.Mappers import org.onap.cps.ncmp.api.impl.events.EventsPublisher import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus +import org.onap.cps.ncmp.api.impl.utils.SubscriptionEventCloudMapper import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent.TargetCmHandle import org.onap.cps.ncmp.api.inventory.InventoryPersistence @@ -44,7 +45,6 @@ import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import spock.util.concurrent.BlockingVariable - import java.util.concurrent.TimeUnit @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, SubscriptionEventForwarder]) @@ -60,6 +60,8 @@ class SubscriptionEventForwarderSpec extends MessagingBaseSpec { @SpringBean IMap<String, Set<String>> mockForwardedSubscriptionEventCache = Mock(IMap<String, Set<String>>) @SpringBean + SubscriptionEventCloudMapper subscriptionEventCloudMapper = new SubscriptionEventCloudMapper(new ObjectMapper()) + @SpringBean SubscriptionEventResponseOutcome mockSubscriptionEventResponseOutcome = Mock(SubscriptionEventResponseOutcome) @SpringBean SubscriptionPersistence mockSubscriptionPersistence = Mock(SubscriptionPersistence) @@ -69,8 +71,8 @@ class SubscriptionEventForwarderSpec extends MessagingBaseSpec { ClientSubscriptionEventMapper clientSubscriptionEventMapper = Mappers.getMapper(ClientSubscriptionEventMapper) @Autowired JsonObjectMapper jsonObjectMapper - - def objectMapper = new ObjectMapper() + @Autowired + ObjectMapper objectMapper def 'Forward valid CM create subscription and simulate timeout'() { given: 'an event' diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseConsumerSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseConsumerSpec.groovy index 7cc40cc90e..3e6d7a82d3 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseConsumerSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseConsumerSpec.groovy @@ -26,6 +26,7 @@ import io.cloudevents.CloudEvent import io.cloudevents.core.builder.CloudEventBuilder import org.apache.kafka.clients.consumer.ConsumerRecord import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistenceImpl +import org.onap.cps.ncmp.api.impl.utils.SubscriptionEventResponseCloudMapper import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse import org.onap.cps.ncmp.utils.TestUtils @@ -47,9 +48,10 @@ class SubscriptionEventResponseConsumerSpec extends MessagingBaseSpec { def mockSubscriptionPersistence = Mock(SubscriptionPersistenceImpl) def mockSubscriptionEventResponseMapper = Mock(SubscriptionEventResponseMapper) def mockSubscriptionEventResponseOutcome = Mock(SubscriptionEventResponseOutcome) + def mockSubscriptionEventResponseCloudMapper = new SubscriptionEventResponseCloudMapper(new ObjectMapper()) def objectUnderTest = new SubscriptionEventResponseConsumer(mockForwardedSubscriptionEventCache, - mockSubscriptionPersistence, mockSubscriptionEventResponseMapper, mockSubscriptionEventResponseOutcome) + mockSubscriptionPersistence, mockSubscriptionEventResponseMapper, mockSubscriptionEventResponseOutcome, mockSubscriptionEventResponseCloudMapper) def 'Consume Subscription Event Response where all DMIs have responded'() { given: 'a consumer record including cloud event having subscription response' diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapperSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapperSpec.groovy index 4c60281f85..d07d9bb993 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapperSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseMapperSpec.groovy @@ -50,11 +50,11 @@ class SubscriptionEventResponseMapperSpec extends Specification { and: 'subscription name' assert result.subscriptionName == "cm-subscription-001" and: 'predicate targets cm handle size as expected' - assert result.predicates.targetCmHandles.size() == 7 + assert result.predicates.targetCmHandles.size() == 4 and: 'predicate targets cm handle ids as expected' - assert result.predicates.targetCmHandles.cmHandleId == ["CMHandle1", "CMHandle2", "CMHandle3", "CMHandle4", "CMHandle5", "CMHandle6", "CMHandle7"] + assert result.predicates.targetCmHandles.cmHandleId == ["CMHandle1", "CMHandle2", "CMHandle3", "CMHandle4"] and: 'the status for these targets is set to expected values' - assert result.predicates.targetCmHandles.status == [SubscriptionStatus.REJECTED, SubscriptionStatus.REJECTED, SubscriptionStatus.REJECTED, SubscriptionStatus.REJECTED, SubscriptionStatus.PENDING, SubscriptionStatus.PENDING, SubscriptionStatus.PENDING] + assert result.predicates.targetCmHandles.status == [SubscriptionStatus.REJECTED, SubscriptionStatus.REJECTED, SubscriptionStatus.PENDING, SubscriptionStatus.PENDING] } }
\ No newline at end of file diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseOutcomeSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseOutcomeSpec.groovy index c1c428b13f..a6c8712a3d 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseOutcomeSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionEventResponseOutcomeSpec.groovy @@ -49,6 +49,8 @@ class SubscriptionEventResponseOutcomeSpec extends DataNodeBaseSpec { EventsPublisher<CloudEvent> mockSubscriptionEventOutcomePublisher = Mock(EventsPublisher<CloudEvent>) @SpringBean SubscriptionOutcomeMapper subscriptionOutcomeMapper = Mappers.getMapper(SubscriptionOutcomeMapper) + @SpringBean + SubscriptionOutcomeCloudMapper subscriptionOutcomeCloudMapper = new SubscriptionOutcomeCloudMapper(new ObjectMapper()) @Autowired JsonObjectMapper jsonObjectMapper diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionOutcomeMapperSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionOutcomeMapperSpec.groovy index f5fbdfcb56..ea1b9e7712 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionOutcomeMapperSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/events/avcsubscription/SubscriptionOutcomeMapperSpec.groovy @@ -48,14 +48,14 @@ class SubscriptionOutcomeMapperSpec extends Specification { def result = objectUnderTest.toSubscriptionEventOutcome(subscriptionResponseEvent) then: 'the resulting subscription event outcome contains expected pending targets per details grouping' def pendingCmHandleTargetsPerDetails = result.getData().getAdditionalInfo().getPending() - assert pendingCmHandleTargetsPerDetails.get(0).getDetails() == 'EMS or node connectivity issues, retrying' - assert pendingCmHandleTargetsPerDetails.get(0).getTargets() == ['CMHandle5', 'CMHandle6','CMHandle7'] + assert pendingCmHandleTargetsPerDetails.get(0).getDetails() == 'No reply from DMI yet' + assert pendingCmHandleTargetsPerDetails.get(0).getTargets() == ['CMHandle3', 'CMHandle4'] and: 'the resulting subscription event outcome contains expected rejected targets per details grouping' def rejectedCmHandleTargetsPerDetails = result.getData().getAdditionalInfo().getRejected() - assert rejectedCmHandleTargetsPerDetails.get(0).getDetails() == 'Target(s) do not exist' - assert rejectedCmHandleTargetsPerDetails.get(0).getTargets() == ['CMHandle4'] - assert rejectedCmHandleTargetsPerDetails.get(1).getDetails() == 'Faulty subscription format for target(s)' - assert rejectedCmHandleTargetsPerDetails.get(1).getTargets() == ['CMHandle1', 'CMHandle2','CMHandle3'] + assert rejectedCmHandleTargetsPerDetails.get(0).getDetails() == 'Some other error message from the DMI' + assert rejectedCmHandleTargetsPerDetails.get(0).getTargets() == ['CMHandle2'] + assert rejectedCmHandleTargetsPerDetails.get(1).getDetails() == 'Some error message from the DMI' + assert rejectedCmHandleTargetsPerDetails.get(1).getTargets() == ['CMHandle1'] } def 'Map subscription event response with null of subscription status list to subscription event outcome causes an exception'() { diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventCloudMapperSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventCloudMapperSpec.groovy index 4023441293..ae61749644 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventCloudMapperSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventCloudMapperSpec.groovy @@ -20,6 +20,7 @@ package org.onap.cps.ncmp.api.impl.utils +import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper import io.cloudevents.core.builder.CloudEventBuilder import org.onap.cps.ncmp.events.avcsubscription1_0_0.client_to_ncmp.SubscriptionEvent @@ -38,6 +39,10 @@ class SubscriptionEventCloudMapperSpec extends Specification { @Autowired ObjectMapper objectMapper + def spyObjectMapper = Spy(ObjectMapper) + + def objectUnderTest = new SubscriptionEventCloudMapper(spyObjectMapper) + def 'Map the data of the cloud event to subscription event'() { given: 'a cloud event having a subscription event in the data part' def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json') @@ -49,7 +54,7 @@ class SubscriptionEventCloudMapperSpec extends Specification { .withSource(URI.create('some-resource')) .withExtension('correlationid', 'test-cmhandle1').build() when: 'the cloud event map to subscription event' - def resultSubscriptionEvent = SubscriptionEventCloudMapper.toSubscriptionEvent(testCloudEvent) + def resultSubscriptionEvent = objectUnderTest.toSubscriptionEvent(testCloudEvent) then: 'the subscription event resulted having expected values' resultSubscriptionEvent.getData() == testEventData.getData() } @@ -63,7 +68,7 @@ class SubscriptionEventCloudMapperSpec extends Specification { .withSource(URI.create('some-resource')) .withExtension('correlationid', 'test-cmhandle1').build() when: 'the cloud event map to subscription event' - def resultSubscriptionEvent = SubscriptionEventCloudMapper.toSubscriptionEvent(testCloudEvent) + def resultSubscriptionEvent = objectUnderTest.toSubscriptionEvent(testCloudEvent) then: 'the subscription event resulted having a null value' resultSubscriptionEvent == null } @@ -81,7 +86,7 @@ class SubscriptionEventCloudMapperSpec extends Specification { .withExtension('correlationid', 'test-cmhandle1').build() when: 'the subscription event map to data of cloud event' SubscriptionEventCloudMapper.randomId = 'some-id' - def resultCloudEvent = SubscriptionEventCloudMapper.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreated') + def resultCloudEvent = objectUnderTest.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreated') then: 'the subscription event resulted having expected values' resultCloudEvent.getData() == testCloudEvent.getData() resultCloudEvent.getId() == testCloudEvent.getId() @@ -90,14 +95,20 @@ class SubscriptionEventCloudMapperSpec extends Specification { resultCloudEvent.getDataSchema() == URI.create('urn:cps:org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent:1.0.0') } - def 'Map the subscription event to data of the cloud event with wrong content causes an exception'() { - given: 'an empty ncmp subscription event' - def testNcmpSubscriptionEvent = new org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent() - when: 'the subscription event map to data of cloud event' - SubscriptionEventCloudMapper.toCloudEvent(testNcmpSubscriptionEvent, 'some-key', 'some-event-type') - then: 'a run time exception is thrown' - def exception = thrown(CloudEventConstructionException) - exception.details == 'Invalid object to serialize or required headers is missing' + def 'Map the subscription event to cloud event with JSON processing exception'() { + given: 'a json processing exception during process' + def jsonProcessingException = new JsonProcessingException('The Cloud Event could not be constructed') + spyObjectMapper.writeValueAsBytes(_) >> { throw jsonProcessingException } + and: 'a subscription event of ncmp version' + def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEventNcmpVersion.json') + def testEventData = jsonObjectMapper.convertJsonString(jsonData, + org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent.class) + when: 'the subscription event map to cloud event' + def expectedResult = objectUnderTest.toCloudEvent(testEventData, 'some-key', 'some-event-type') + then: 'no exception is thrown since it has been handled already' + noExceptionThrown() + and: 'expected result should be null' + expectedResult == null } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventResponseCloudMapperSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventResponseCloudMapperSpec.groovy new file mode 100644 index 0000000000..9dcd0a6459 --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionEventResponseCloudMapperSpec.groovy @@ -0,0 +1,74 @@ +/* + * ============LICENSE_START======================================================== + * Copyright (c) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.utils + +import com.fasterxml.jackson.databind.ObjectMapper +import io.cloudevents.core.builder.CloudEventBuilder +import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse +import org.onap.cps.ncmp.utils.TestUtils +import org.onap.cps.utils.JsonObjectMapper +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import spock.lang.Specification + +@SpringBootTest(classes = [ObjectMapper, JsonObjectMapper]) +class SubscriptionEventResponseCloudMapperSpec extends Specification { + + @Autowired + JsonObjectMapper jsonObjectMapper + + @Autowired + ObjectMapper objectMapper + + def spyObjectMapper = Spy(ObjectMapper) + + def objectUnderTest = new SubscriptionEventResponseCloudMapper(spyObjectMapper) + + def 'Map the cloud event to subscription event response'() { + given: 'a cloud event having a subscription event response in the data part' + def jsonData = TestUtils.getResourceFileContent('avcSubscriptionEventResponse.json') + def testEventData = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEventResponse.class) + def testCloudEvent = CloudEventBuilder.v1() + .withData(objectMapper.writeValueAsBytes(testEventData)) + .withId('some-event-id') + .withType('subscriptionCreatedStatus') + .withSource(URI.create('some-resource')) + .withExtension('correlationid', 'test-cmhandle1').build() + when: 'the cloud event map to subscription event response' + def resultSubscriptionEvent = objectUnderTest.toSubscriptionEventResponse(testCloudEvent) + then: 'the subscription event resulted having expected values' + resultSubscriptionEvent.getData() == testEventData.getData() + } + + def 'Map null of the data of the cloud event to subscription event response'() { + given: 'a cloud event having a null subscription event response in the data part' + def testCloudEvent = CloudEventBuilder.v1() + .withData(null) + .withId('some-event-id') + .withType('subscriptionCreatedStatus') + .withSource(URI.create('some-resource')) + .withExtension('correlationid', 'test-cmhandle1').build() + when: 'the cloud event map to subscription event response' + def resultSubscriptionEvent = objectUnderTest.toSubscriptionEventResponse(testCloudEvent) + then: 'the subscription event response resulted having a null value' + resultSubscriptionEvent == null + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionOutcomeCloudMapperSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionOutcomeCloudMapperSpec.groovy new file mode 100644 index 0000000000..ac055b5f6e --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/SubscriptionOutcomeCloudMapperSpec.groovy @@ -0,0 +1,82 @@ +/* + * ============LICENSE_START======================================================== + * Copyright (c) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.ncmp.api.impl.utils + +import com.fasterxml.jackson.core.JsonProcessingException +import com.fasterxml.jackson.databind.ObjectMapper +import io.cloudevents.core.builder.CloudEventBuilder +import org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_client.SubscriptionEventOutcome +import org.onap.cps.ncmp.utils.TestUtils +import org.onap.cps.utils.JsonObjectMapper +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import spock.lang.Specification + +@SpringBootTest(classes = [ObjectMapper, JsonObjectMapper]) +class SubscriptionOutcomeCloudMapperSpec extends Specification { + + @Autowired + JsonObjectMapper jsonObjectMapper + + @Autowired + ObjectMapper objectMapper + + def spyObjectMapper = Spy(ObjectMapper) + + def objectUnderTest = new SubscriptionOutcomeCloudMapper(spyObjectMapper) + + def 'Map the subscription outcome to cloud event'() { + given: 'a subscription event' + def jsonData = TestUtils.getResourceFileContent('avcSubscriptionOutcomeEvent.json') + def testEventData = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEventOutcome.class) + def testCloudEvent = CloudEventBuilder.v1() + .withData(objectMapper.writeValueAsBytes(testEventData)) + .withId('some-id') + .withType('subscriptionCreatedStatus') + .withSource(URI.create('NCMP')) + .withExtension('correlationid', 'test-cmhandle1').build() + when: 'the subscription event map to data of cloud event' + SubscriptionOutcomeCloudMapper.randomId = 'some-id' + def resultCloudEvent = objectUnderTest.toCloudEvent(testEventData, 'some-event-key', 'subscriptionCreatedStatus') + then: 'the subscription event resulted having expected values' + resultCloudEvent.getData() == testCloudEvent.getData() + resultCloudEvent.getId() == testCloudEvent.getId() + resultCloudEvent.getType() == testCloudEvent.getType() + resultCloudEvent.getSource() == testCloudEvent.getSource() + resultCloudEvent.getDataSchema() == URI.create('urn:cps:org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_client.SubscriptionEventOutcome:1.0.0') + } + + def 'Map the subscription outcome to cloud event with JSON processing exception'() { + given: 'a json processing exception during process' + def jsonProcessingException = new JsonProcessingException('The Cloud Event could not be constructed') + spyObjectMapper.writeValueAsBytes(_) >> { throw jsonProcessingException } + and: 'a cloud event having a subscription outcome in the data part' + def jsonData = TestUtils.getResourceFileContent('avcSubscriptionOutcomeEvent.json') + def testEventData = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEventOutcome.class) + when: 'the subscription outcome map to cloud event' + def expectedResult = objectUnderTest.toCloudEvent(testEventData, 'some-key', 'some-event-type') + then: 'no exception is thrown since it has been handled already' + noExceptionThrown() + and: 'expected result should be null' + expectedResult == null + } + +} |