Fix: Make bookstore data consistent
[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.apache.kafka.common.header.internals.RecordHeaders
25 import org.mapstruct.factory.Mappers
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.subscriptions.SubscriptionStatus
29 import org.onap.cps.ncmp.api.impl.utils.DataNodeBaseSpec
30 import org.onap.cps.ncmp.events.avc.subscription.v1.SubscriptionEventOutcome
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.spockframework.spring.SpringBean
34 import org.springframework.beans.factory.annotation.Autowired
35 import org.springframework.boot.test.context.SpringBootTest
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 'Send response to the client apps successfully'() {
54         given: 'a subscription client id and subscription name'
55             def clientId = 'some-client-id'
56             def subscriptionName = 'some-subscription-name'
57         and: 'the persistence service return a data node'
58             mockSubscriptionPersistence.getCmHandlesForSubscriptionEvent(*_) >> [dataNode4]
59         and: 'the response is being generated from the db'
60             def eventOutcome = objectUnderTest.generateResponse(clientId, subscriptionName)
61         when: 'the response is being sent'
62             objectUnderTest.sendResponse(clientId, subscriptionName)
63         then: 'the publisher publish the response with expected parameters'
64             1 * mockSubscriptionEventOutcomePublisher.publishEvent('cm-avc-subscription-response', clientId + subscriptionName, new RecordHeaders(), eventOutcome)
65     }
66
67     def 'Check cm handle id to status map to see if it is a full outcome response'() {
68         when: 'is full outcome response evaluated'
69             def response = objectUnderTest.isFullOutcomeResponse(cmHandleIdToStatusMap)
70         then: 'the result will be as expected'
71             response == expectedResult
72         where: 'the following values are used'
73             scenario                                          | cmHandleIdToStatusMap                                                                       || expectedResult
74             'The map contains PENDING status'                 | ['CMHandle1': SubscriptionStatus.PENDING] as Map                                            || false
75             'The map contains ACCEPTED status'                | ['CMHandle1': SubscriptionStatus.ACCEPTED] as Map                                           || true
76             'The map contains REJECTED status'                | ['CMHandle1': SubscriptionStatus.REJECTED] as Map                                           || true
77             'The map contains PENDING and ACCEPTED statuses'  | ['CMHandle1': SubscriptionStatus.PENDING,'CMHandle2': SubscriptionStatus.ACCEPTED] as Map   || false
78             'The map contains REJECTED and ACCEPTED statuses' | ['CMHandle1': SubscriptionStatus.REJECTED,'CMHandle2': SubscriptionStatus.ACCEPTED] as Map  || true
79             'The map contains PENDING and REJECTED statuses'  | ['CMHandle1': SubscriptionStatus.PENDING,'CMHandle2': SubscriptionStatus.REJECTED] as Map   || false
80     }
81
82     def 'Generate response via fetching data nodes from database.'() {
83         given: 'a db call to get data nodes for subscription event'
84             1 * mockSubscriptionPersistence.getCmHandlesForSubscriptionEvent(*_) >> [dataNode4]
85         when: 'a response is generated'
86             def result = objectUnderTest.generateResponse('some-client-id', 'some-subscription-name')
87         then: 'the result will have the same values as same as in dataNode4'
88             result.eventType == SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME
89             result.getEvent().getSubscription().getClientID() == 'some-client-id'
90             result.getEvent().getSubscription().getName() == 'some-subscription-name'
91             result.getEvent().getPredicates().getPendingTargets() == ['CMHandle3']
92             result.getEvent().getPredicates().getRejectedTargets() == ['CMHandle1']
93             result.getEvent().getPredicates().getAcceptedTargets() == ['CMHandle2']
94     }
95
96     def 'Form subscription outcome message with a list of cm handle id to status mapping'() {
97         given: 'a list of collection including cm handle id to status'
98             def cmHandleIdToStatus = [['PENDING', 'CMHandle5'], ['PENDING', 'CMHandle4'], ['ACCEPTED', 'CMHandle1'], ['REJECTED', 'CMHandle3']]
99         and: 'an outcome event'
100             def jsonData = TestUtils.getResourceFileContent('avcSubscriptionOutcomeEvent.json')
101             def eventOutcome = jsonObjectMapper.convertJsonString(jsonData, SubscriptionEventOutcome.class)
102             eventOutcome.setEventType(expectedEventType)
103         when: 'a subscription outcome message formed'
104             def result = objectUnderTest.formSubscriptionOutcomeMessage(cmHandleIdToStatus, 'SCO-9989752',
105                 'cm-subscription-001', isFullOutcomeResponse)
106             result.getEvent().getPredicates().getPendingTargets().sort()
107         then: 'the result will be equal to event outcome'
108             result == eventOutcome
109         where: 'the following values are used'
110             scenario             | isFullOutcomeResponse || expectedEventType
111             'is full outcome'    | true                  || SubscriptionEventOutcome.EventType.COMPLETE_OUTCOME
112             'is partial outcome' | false                 || SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME
113     }
114 }