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