Subscription Create Event Outcome Kafka Part
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / avcsubscription / SubscriptionEventResponseOutcomeSpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.avcsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.mapstruct.factory.Mappers
25 import org.onap.cps.ncmp.api.impl.event.avc.SubscriptionOutcomeMapper
26 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
27 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
28 import org.onap.cps.ncmp.api.impl.utils.DataNodeBaseSpec
29 import org.onap.cps.ncmp.events.avc.subscription.v1.SubscriptionEventOutcome
30 import org.onap.cps.ncmp.utils.TestUtils
31 import org.onap.cps.utils.JsonObjectMapper
32 import org.spockframework.spring.SpringBean
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.boot.test.context.SpringBootTest
35 import org.testcontainers.shaded.org.bouncycastle.crypto.engines.EthereumIESEngine
36
37 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper, SubscriptionOutcomeMapper, SubscriptionEventResponseOutcome])
38 class SubscriptionEventResponseOutcomeSpec extends DataNodeBaseSpec {
39
40     @Autowired
41     SubscriptionEventResponseOutcome objectUnderTest
42
43     @SpringBean
44     SubscriptionPersistence mockSubscriptionPersistence = Mock(SubscriptionPersistence)
45     @SpringBean
46     EventsPublisher<SubscriptionEventOutcome> mockSubscriptionEventOutcomePublisher = Mock(EventsPublisher<SubscriptionEventOutcome>)
47     @SpringBean
48     SubscriptionOutcomeMapper subscriptionOutcomeMapper = Mappers.getMapper(SubscriptionOutcomeMapper)
49
50     @Autowired
51     JsonObjectMapper jsonObjectMapper
52
53     def 'Generate response via fetching data nodes from database.'() {
54         given: 'a db call to get data nodes for subscription event'
55             1 * mockSubscriptionPersistence.getDataNodesForSubscriptionEvent() >> [dataNode4]
56         when: 'a response is generated'
57             def result = objectUnderTest.generateResponse('some-client-id', 'some-subscription-name', isFullOutcomeResponse)
58         then: 'the result will have the same values as same as in dataNode4'
59             result.eventType == eventType
60             result.getEvent().getSubscription().getClientID() == 'some-client-id'
61             result.getEvent().getSubscription().getName() == 'some-subscription-name'
62             result.getEvent().getPredicates().getPendingTargets() == ['CMHandle3']
63             result.getEvent().getPredicates().getRejectedTargets() == ['CMHandle1']
64             result.getEvent().getPredicates().getAcceptedTargets() == ['CMHandle2']
65         where: 'the following values are used'
66             scenario             | isFullOutcomeResponse || eventType
67             'is full outcome'    | true                  || SubscriptionEventOutcome.EventType.COMPLETE_OUTCOME
68             'is partial outcome' | false                 || SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME
69     }
70
71     def 'Form subscription outcome message with a list of cm handle id to status mapping'() {
72         given: 'a list of collection including cm handle id to status'
73             def cmHandleIdToStatus = [['PENDING', 'CMHandle5'], ['PENDING', 'CMHandle4'], ['ACCEPTED', 'CMHandle1'], ['REJECTED', 'CMHandle3']]
74         and: 'an outcome event'
75             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionOutcomeEvent.json')
76             def eventOutcome = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEventOutcome.class)
77             eventOutcome.setEventType(eventType)
78         when: 'a subscription outcome message formed'
79             def result = objectUnderTest.formSubscriptionOutcomeMessage(cmHandleIdToStatus, 'SCO-9989752',
80                 'cm-subscription-001', isFullOutcomeResponse)
81             result.getEvent().getPredicates().getPendingTargets().sort()
82         then: 'the result will be equal to event outcome'
83             result == eventOutcome
84         where: 'the following values are used'
85             scenario             | isFullOutcomeResponse | eventType
86             'is full outcome'    | true                  | SubscriptionEventOutcome.EventType.COMPLETE_OUTCOME
87             'is partial outcome' | false                 | SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME
88     }
89 }