42bb9d784051bb44c1d7b18863cff4e807d4a23f
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-2026 OpenInfra Foundation Europe. All rights reserved.
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.impl.inventory.sync.lcm
22
23 import io.micrometer.core.instrument.Tag
24 import io.micrometer.core.instrument.simple.SimpleMeterRegistry
25 import org.onap.cps.events.EventProducer
26 import org.onap.cps.ncmp.api.inventory.models.CompositeState
27 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle
28 import org.springframework.kafka.KafkaException
29 import spock.lang.Specification
30
31 import static org.onap.cps.ncmp.api.inventory.models.CmHandleState.ADVISED
32
33 class LcmEventProducerSpec extends Specification {
34
35     def mockEventProducer = Mock(EventProducer)
36     def lcmEventObjectCreator = new LcmEventObjectCreator()
37     def meterRegistry = new SimpleMeterRegistry()
38
39     def objectUnderTest = new LcmEventProducer(mockEventProducer, lcmEventObjectCreator, meterRegistry)
40
41     def cmHandleTransitionPair = new CmHandleTransitionPair(
42         new YangModelCmHandle(id: 'ch-1',  additionalProperties: [], publicProperties: []),
43         new YangModelCmHandle(id: 'ch-1', compositeState: new CompositeState(cmHandleState: ADVISED), additionalProperties: [], publicProperties: [])
44     )
45
46     def 'Create and send lcm event #eventVersion where notifications are #scenario.'() {
47         given: 'notificationsEnabled is #notificationsEnabled'
48             objectUnderTest.notificationsEnabled = notificationsEnabled
49             objectUnderTest.eventSchemaVersion = eventVersion
50         when: 'event send for (batch of) 1 cm handle transition pair (new cm handle going to READY)'
51             objectUnderTest.sendLcmEventBatchAsynchronously([cmHandleTransitionPair])
52         then: 'producer is called #expectedTimesMethodCalled times with correct identifiers'
53             expectedTimesMethodCalled * mockEventProducer.sendLegacyEvent(_, 'ch-1', _, _) >> {
54                 args -> {
55                     def eventHeaders = args[2]
56                     def event = args[3]
57                     assert UUID.fromString(eventHeaders.get('eventId')) != null
58                     assert eventHeaders.get('eventCorrelationId') == 'ch-1'
59                     assert event.class.simpleName == expectedEventClass
60                 }
61             }
62         and: 'metrics are recorded with correct tags'
63             def timer = meterRegistry.find('cps.ncmp.lcm.events.send').timer()
64             if (notificationsEnabled) {
65                 assert timer.count() == 1
66                 assert timer.id.tags.containsAll(Tag.of('oldCmHandleState', 'N/A'), Tag.of('newCmHandleState', 'ADVISED'))
67             } else {
68                 assert timer == null
69             }
70         where: 'the following values are used'
71             scenario   | eventVersion | notificationsEnabled || expectedTimesMethodCalled | expectedEventClass
72             'enabled'  | 'v1'         | true                 || 1                         | 'LcmEventV1'
73             'enabled'  | 'v2'         | true                 || 1                         | 'LcmEventV2'
74             'disabled' | 'v1'         | false                || 0                         | 'N/A'
75     }
76
77     def 'Exception while sending message.'(){
78         given: 'notifications are enabled'
79             objectUnderTest.notificationsEnabled = true
80         when: 'producer set to throw an exception'
81             mockEventProducer.sendLegacyEvent(*_) >> { throw new KafkaException('sending failed')}
82         and: 'attempt to send events'
83             objectUnderTest.sendLcmEventBatchAsynchronously([cmHandleTransitionPair])
84         then: 'the exception is just logged and not bubbled up'
85             noExceptionThrown()
86         and: 'metrics are not recorded'
87             assert  meterRegistry.find('cps.ncmp.lcm.events.send').timer() == null
88     }
89
90 }