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.events.cmsubscription
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.core.builder.CloudEventBuilder
26 import org.mapstruct.factory.Mappers
27 import org.onap.cps.ncmp.api.NcmpEventResponseCode
28 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
29 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
30 import org.onap.cps.ncmp.api.impl.utils.DataNodeBaseSpec
31 import org.onap.cps.ncmp.api.impl.utils.SubscriptionOutcomeCloudMapper
32 import org.onap.cps.ncmp.events.cmsubscription1_0_0.dmi_to_ncmp.CmSubscriptionDmiOutEvent
33 import org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_client.CmSubscriptionNcmpOutEvent
34 import org.onap.cps.ncmp.utils.TestUtils
35 import org.onap.cps.utils.JsonObjectMapper
36 import org.spockframework.spring.SpringBean
37 import org.springframework.beans.factory.annotation.Autowired
38 import org.springframework.boot.test.context.SpringBootTest
40 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, CmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapper, CmSubscriptionNcmpOutEventPublisher])
41 class CmSubscriptionNcmpOutEventPublisherSpec extends DataNodeBaseSpec {
44 CmSubscriptionNcmpOutEventPublisher objectUnderTest
47 SubscriptionPersistence mockSubscriptionPersistence = Mock(SubscriptionPersistence)
49 EventsPublisher<CloudEvent> mockCmSubscriptionNcmpOutEventPublisher = Mock(EventsPublisher<CloudEvent>)
51 CmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapper cmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapper = Mappers.getMapper(CmSubscriptionDmiOutEventToCmSubscriptionNcmpOutEventMapper)
53 SubscriptionOutcomeCloudMapper subscriptionOutcomeCloudMapper = new SubscriptionOutcomeCloudMapper(new ObjectMapper())
56 JsonObjectMapper jsonObjectMapper
59 ObjectMapper objectMapper
61 def 'Send response to the client apps successfully'() {
62 given: 'a subscription response event'
63 def subscriptionResponseJsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
64 def subscriptionResponseEvent = jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, CmSubscriptionDmiOutEvent.class)
65 and: 'a subscription outcome event'
66 def subscriptionOutcomeJsonData = TestUtils.getResourceFileContent('cmSubscriptionNcmpOutEvent2.json')
67 def subscriptionOutcomeEvent = jsonObjectMapper.convertJsonString(subscriptionOutcomeJsonData, CmSubscriptionNcmpOutEvent.class)
68 and: 'a random id for the cloud event'
69 SubscriptionOutcomeCloudMapper.randomId = 'some-id'
70 and: 'a cloud event containing the outcome event'
71 def testCloudEventSent = CloudEventBuilder.v1()
72 .withData(objectMapper.writeValueAsBytes(subscriptionOutcomeEvent))
74 .withType('subscriptionCreatedStatus')
75 .withDataSchema(URI.create('urn:cps:' + 'org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_client.CmSubscriptionNcmpOutEvent' + ':1.0.0'))
76 .withExtension("correlationid", 'SCO-9989752cm-subscription-001')
77 .withSource(URI.create('NCMP')).build()
78 and: 'the persistence service return a data node that includes pending cm handles that makes it partial success'
79 mockSubscriptionPersistence.getCmHandlesForSubscriptionEvent(*_) >> [dataNode4]
80 when: 'the response is being sent'
81 objectUnderTest.sendResponse(subscriptionResponseEvent, 'subscriptionCreatedStatus')
82 then: 'the publisher publish the cloud event with itself and expected parameters'
83 1 * mockCmSubscriptionNcmpOutEventPublisher.publishCloudEvent('subscription-response', 'SCO-9989752cm-subscription-001', testCloudEventSent)
86 def 'Create subscription outcome message as expected'() {
87 given: 'a subscription response event'
88 def subscriptionResponseJsonData = TestUtils.getResourceFileContent('cmSubscriptionDmiOutEvent.json')
89 def subscriptionResponseEvent = jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, CmSubscriptionDmiOutEvent.class)
90 and: 'a subscription outcome event'
91 def subscriptionOutcomeJsonData = TestUtils.getResourceFileContent('cmSubscriptionNcmpOutEvent.json')
92 def subscriptionOutcomeEvent = jsonObjectMapper.convertJsonString(subscriptionOutcomeJsonData, CmSubscriptionNcmpOutEvent.class)
93 and: 'a status code and status message a per #scenarios'
94 subscriptionOutcomeEvent.getData().setStatusCode(statusCode)
95 subscriptionOutcomeEvent.getData().setStatusMessage(statusMessage)
96 when: 'a subscription event outcome message is being formed'
97 def result = objectUnderTest.fromDmiOutEvent(subscriptionResponseEvent, ncmpEventResponseCode)
98 then: 'the result will be equal to event outcome'
99 result == subscriptionOutcomeEvent
100 where: 'the following values are used'
101 scenario | ncmpEventResponseCode || statusMessage || statusCode
102 'is full outcome' | NcmpEventResponseCode.SUCCESSFULLY_APPLIED_SUBSCRIPTION || 'successfully applied subscription' || 1
103 'is partial outcome' | NcmpEventResponseCode.PARTIALLY_APPLIED_SUBSCRIPTION || 'partially applied subscription' || 104
106 def 'Check cm handle id to status map to see if it is a full outcome response'() {
107 when: 'is full outcome response evaluated'
108 def response = objectUnderTest.decideOnNcmpEventResponseCodeForSubscription(cmHandleIdToStatusAndDetailsAsMap)
109 then: 'the result will be as expected'
110 response == expectedOutcomeResponseDecision
111 where: 'the following values are used'
112 scenario | cmHandleIdToStatusAndDetailsAsMap || expectedOutcomeResponseDecision
113 'The map contains PENDING status' | [CMHandle1: [details:'Subscription forwarded to dmi plugin',status:'PENDING'] as Map] as Map || NcmpEventResponseCode.SUBSCRIPTION_PENDING
114 'The map contains ACCEPTED status' | [CMHandle1: [details:'',status:'ACCEPTED'] as Map] as Map || NcmpEventResponseCode.SUCCESSFULLY_APPLIED_SUBSCRIPTION
115 'The map contains REJECTED status' | [CMHandle1: [details:'Cm handle does not exist',status:'REJECTED'] as Map] as Map || NcmpEventResponseCode.SUBSCRIPTION_NOT_APPLICABLE
116 'The map contains PENDING and PENDING statuses' | [CMHandle1: [details:'Some details',status:'PENDING'] as Map, CMHandle2: [details:'Some details',status:'PENDING'] as Map as Map] as Map || NcmpEventResponseCode.SUBSCRIPTION_PENDING
117 'The map contains ACCEPTED and ACCEPTED statuses' | [CMHandle1: [details:'',status:'ACCEPTED'] as Map, CMHandle2: [details:'',status:'ACCEPTED'] as Map as Map] as Map || NcmpEventResponseCode.SUCCESSFULLY_APPLIED_SUBSCRIPTION
118 'The map contains REJECTED and REJECTED statuses' | [CMHandle1: [details:'Reject details',status:'REJECTED'] as Map, CMHandle2: [details:'Reject details',status:'REJECTED'] as Map as Map] as Map || NcmpEventResponseCode.SUBSCRIPTION_NOT_APPLICABLE
119 'The map contains PENDING and ACCEPTED statuses' | [CMHandle1: [details:'Some details',status:'PENDING'] as Map, CMHandle2: [details:'',status:'ACCEPTED'] as Map as Map] as Map || NcmpEventResponseCode.PARTIALLY_APPLIED_SUBSCRIPTION
120 'The map contains REJECTED and ACCEPTED statuses' | [CMHandle1: [details:'Cm handle does not exist',status:'REJECTED'] as Map, CMHandle2: [details:'',status:'ACCEPTED'] as Map as Map] as Map || NcmpEventResponseCode.PARTIALLY_APPLIED_SUBSCRIPTION
121 'The map contains PENDING and REJECTED statuses' | [CMHandle1: [details:'Subscription forwarded to dmi plugin',status:'PENDING'] as Map, CMHandle2: [details:'Cm handle does not exist',status:'REJECTED'] as Map as Map] as Map || NcmpEventResponseCode.PARTIALLY_APPLIED_SUBSCRIPTION