Merge "Modify cmHandle registration for alternateId"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / events / cmsubscription / CmSubscriptionNcmpInEventConsumerSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2022-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.cmsubscription
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.core.builder.CloudEventBuilder
26 import org.apache.kafka.clients.consumer.ConsumerRecord
27 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent
29 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
30 import org.onap.cps.ncmp.events.cmsubscription1_0_0.client_to_ncmp.CmSubscriptionNcmpInEvent
31 import org.onap.cps.ncmp.utils.TestUtils
32 import org.onap.cps.utils.JsonObjectMapper
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.boot.test.context.SpringBootTest
35
36 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
37 class CmSubscriptionNcmpInEventConsumerSpec extends MessagingBaseSpec {
38
39     def mockCmSubscriptionNcmpInEventForwarder = Mock(CmSubscriptionNcmpInEventForwarder)
40     def mockCmSubscriptionNcmpInEventMapper = Mock(CmSubscriptionNcmpInEventMapper)
41     def mockSubscriptionPersistence = Mock(SubscriptionPersistence)
42     def objectUnderTest = new CmSubscriptionNcmpInEventConsumer(mockCmSubscriptionNcmpInEventForwarder, mockCmSubscriptionNcmpInEventMapper, mockSubscriptionPersistence)
43
44     def yangModelSubscriptionEvent = new YangModelSubscriptionEvent()
45
46     @Autowired
47     JsonObjectMapper jsonObjectMapper
48
49     @Autowired
50     ObjectMapper objectMapper
51
52
53     def 'Consume, persist and forward valid CM create message'() {
54         given: 'an event with data category CM'
55             def jsonData = TestUtils.getResourceFileContent('cmSubscriptionNcmpInEvent.json')
56             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmSubscriptionNcmpInEvent.class)
57             testEventSent.getData().getDataType().setDataCategory(dataCategory)
58             def testCloudEventSent = CloudEventBuilder.v1()
59                 .withData(objectMapper.writeValueAsBytes(testEventSent))
60                 .withId('subscriptionCreated')
61                 .withType(dataType)
62                 .withSource(URI.create('some-resource'))
63                 .withExtension('correlationid', 'test-cmhandle1').build()
64             def consumerRecord = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
65         and: 'notifications are enabled'
66             objectUnderTest.notificationFeatureEnabled = isNotificationEnabled
67         and: 'subscription model loader is enabled'
68             objectUnderTest.subscriptionModelLoaderEnabled = isModelLoaderEnabled
69         when: 'the valid event is consumed'
70             objectUnderTest.consumeSubscriptionEvent(consumerRecord)
71         then: 'the event is mapped to a yangModelSubscription'
72             numberOfTimesToPersist * mockCmSubscriptionNcmpInEventMapper.toYangModelSubscriptionEvent(testEventSent) >> yangModelSubscriptionEvent
73         and: 'the event is persisted'
74             numberOfTimesToPersist * mockSubscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent)
75         and: 'the event is forwarded'
76             numberOfTimesToForward * mockCmSubscriptionNcmpInEventForwarder.forwardCreateSubscriptionEvent(testEventSent, 'subscriptionCreated')
77         where: 'given values are used'
78             scenario                                            |  dataCategory  |   dataType                  |  isNotificationEnabled     |   isModelLoaderEnabled      ||     numberOfTimesToForward        ||      numberOfTimesToPersist
79             'Both model loader and notification are enabled'    |       'CM'     |   'subscriptionCreated'     |     true                   |        true                 ||         1                         ||             1
80             'Both model loader and notification are disabled'   |       'CM'     |   'subscriptionCreated'     |     false                  |        false                ||         0                         ||             0
81             'Model loader enabled and notification  disabled'   |       'CM'     |   'subscriptionCreated'     |     false                  |        true                 ||         0                         ||             1
82             'Model loader disabled and notification enabled'    |       'CM'     |   'subscriptionCreated'     |     true                   |        false                ||         1                         ||             0
83             'Flags are enabled but data category is FM'         |       'FM'     |   'subscriptionCreated'     |     true                   |        true                 ||         0                         ||             0
84             'Flags are enabled but data type is UPDATE'         |       'CM'     |   'subscriptionUpdated'     |     true                   |        true                 ||         0                         ||             1
85     }
86
87     def 'Consume event with wrong datastore causes an exception'() {
88         given: 'an event'
89             def jsonData = TestUtils.getResourceFileContent('cmSubscriptionNcmpInEvent.json')
90             def testEventSent = jsonObjectMapper.convertJsonString(jsonData, CmSubscriptionNcmpInEvent.class)
91         and: 'datastore is set to a passthrough-running datastore'
92             testEventSent.getData().getPredicates().setDatastore('operational')
93             def testCloudEventSent = CloudEventBuilder.v1()
94                 .withData(objectMapper.writeValueAsBytes(testEventSent))
95                 .withId('some-event-id')
96                 .withType('some-event-type')
97                 .withSource(URI.create('some-resource'))
98                 .withExtension('correlationid', 'test-cmhandle1').build()
99             def consumerRecord = new ConsumerRecord<String, CmSubscriptionNcmpInEvent>('topic-name', 0, 0, 'event-key', testCloudEventSent)
100         when: 'the valid event is consumed'
101             objectUnderTest.consumeSubscriptionEvent(consumerRecord)
102         then: 'an operation not supported exception is thrown'
103             thrown(UnsupportedOperationException)
104     }
105
106 }