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