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.avcsubscription
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.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse
33 import org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_client.SubscriptionEventOutcome;
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, SubscriptionOutcomeMapper, SubscriptionEventResponseOutcome])
41 class SubscriptionEventResponseOutcomeSpec extends DataNodeBaseSpec {
44 SubscriptionEventResponseOutcome objectUnderTest
47 SubscriptionPersistence mockSubscriptionPersistence = Mock(SubscriptionPersistence)
49 EventsPublisher<CloudEvent> mockSubscriptionEventOutcomePublisher = Mock(EventsPublisher<CloudEvent>)
51 SubscriptionOutcomeMapper subscriptionOutcomeMapper = Mappers.getMapper(SubscriptionOutcomeMapper)
54 JsonObjectMapper jsonObjectMapper
57 ObjectMapper objectMapper
59 def 'Send response to the client apps successfully'() {
60 given: 'a subscription response event'
61 def subscriptionResponseJsonData = TestUtils.getResourceFileContent('avcSubscriptionEventResponse.json')
62 def subscriptionResponseEvent = jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, SubscriptionEventResponse.class)
63 and: 'a subscription outcome event'
64 def subscriptionOutcomeJsonData = TestUtils.getResourceFileContent('avcSubscriptionOutcomeEvent2.json')
65 def subscriptionOutcomeEvent = jsonObjectMapper.convertJsonString(subscriptionOutcomeJsonData, SubscriptionEventOutcome.class)
66 and: 'a random id for the cloud event'
67 SubscriptionOutcomeCloudMapper.randomId = 'some-id'
68 and: 'a cloud event containing the outcome event'
69 def testCloudEventSent = CloudEventBuilder.v1()
70 .withData(objectMapper.writeValueAsBytes(subscriptionOutcomeEvent))
72 .withType('subscriptionCreatedStatus')
73 .withDataSchema(URI.create('urn:cps:' + 'org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_client.SubscriptionEventOutcome' + ':1.0.0'))
74 .withExtension("correlationid", 'SCO-9989752cm-subscription-001')
75 .withSource(URI.create('NCMP')).build()
76 and: 'the persistence service return a data node that includes pending cm handles that makes it partial success'
77 mockSubscriptionPersistence.getCmHandlesForSubscriptionEvent(*_) >> [dataNode4]
78 when: 'the response is being sent'
79 objectUnderTest.sendResponse(subscriptionResponseEvent, 'subscriptionCreatedStatus')
80 then: 'the publisher publish the cloud event with itself and expected parameters'
81 1 * mockSubscriptionEventOutcomePublisher.publishCloudEvent('subscription-response', 'SCO-9989752cm-subscription-001', testCloudEventSent)
84 def 'Create subscription outcome message as expected'() {
85 given: 'a subscription response event'
86 def subscriptionResponseJsonData = TestUtils.getResourceFileContent('avcSubscriptionEventResponse.json')
87 def subscriptionResponseEvent = jsonObjectMapper.convertJsonString(subscriptionResponseJsonData, SubscriptionEventResponse.class)
88 and: 'a subscription outcome event'
89 def subscriptionOutcomeJsonData = TestUtils.getResourceFileContent('avcSubscriptionOutcomeEvent.json')
90 def subscriptionOutcomeEvent = jsonObjectMapper.convertJsonString(subscriptionOutcomeJsonData, SubscriptionEventOutcome.class)
91 and: 'a status code and status message a per #scenarios'
92 subscriptionOutcomeEvent.getData().setStatusCode(statusCode)
93 subscriptionOutcomeEvent.getData().setStatusMessage(statusMessage)
94 when: 'a subscription event outcome message is being formed'
95 def result = objectUnderTest.fromSubscriptionEventResponse(subscriptionResponseEvent, ncmpEventResponseCode)
96 then: 'the result will be equal to event outcome'
97 result == subscriptionOutcomeEvent
98 where: 'the following values are used'
99 scenario | ncmpEventResponseCode || statusMessage || statusCode
100 'is full outcome' | NcmpEventResponseCode.SUCCESSFULLY_APPLIED_SUBSCRIPTION || 'successfully applied subscription' || 1
101 'is partial outcome' | NcmpEventResponseCode.PARTIALLY_APPLIED_SUBSCRIPTION || 'partially applied subscription' || 104
104 def 'Check cm handle id to status map to see if it is a full outcome response'() {
105 when: 'is full outcome response evaluated'
106 def response = objectUnderTest.decideOnNcmpEventResponseCodeForSubscription(cmHandleIdToStatusAndDetailsAsMap)
107 then: 'the result will be as expected'
108 response == expectedOutcomeResponseDecision
109 where: 'the following values are used'
110 scenario | cmHandleIdToStatusAndDetailsAsMap || expectedOutcomeResponseDecision
111 'The map contains PENDING status' | [CMHandle1: [details:'Subscription forwarded to dmi plugin',status:'PENDING'] as Map] as Map || NcmpEventResponseCode.SUBSCRIPTION_PENDING
112 'The map contains ACCEPTED status' | [CMHandle1: [details:'',status:'ACCEPTED'] as Map] as Map || NcmpEventResponseCode.SUCCESSFULLY_APPLIED_SUBSCRIPTION
113 'The map contains REJECTED status' | [CMHandle1: [details:'Cm handle does not exist',status:'REJECTED'] as Map] as Map || NcmpEventResponseCode.SUBSCRIPTION_NOT_APPLICABLE
114 '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
115 '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
116 '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
117 '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
118 '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
119 '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