be65e4abfbb5add1c5886738046327f35a4de4b2
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022-2025 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 where notifications are #scenario.'() {
47         given: 'notificationsEnabled is #notificationsEnabled'
48             objectUnderTest.notificationsEnabled = notificationsEnabled
49         when: 'event send for (batch of) 1 cm handle transition pair (new cm handle going to READY)'
50             objectUnderTest.sendLcmEventBatchAsynchronously([cmHandleTransitionPair])
51         then: 'producer is called #expectedTimesMethodCalled times with correct identifiers'
52             expectedTimesMethodCalled * mockEventProducer.sendLegacyEvent(_, 'ch-1', _, _) >> {
53                 args -> {
54                     def eventHeaders = args[2]
55                     assert UUID.fromString(eventHeaders.get('eventId')) != null
56                     assert eventHeaders.get('eventCorrelationId') == 'ch-1'
57                 }
58             }
59         and: 'metrics are recorded with correct tags'
60             def timer = meterRegistry.find('cps.ncmp.lcm.events.send').timer()
61             if (notificationsEnabled) {
62                 assert timer.count() == 1
63                 assert timer.id.tags.containsAll(Tag.of('oldCmHandleState', 'N/A'), Tag.of('newCmHandleState', 'ADVISED'))
64             } else {
65                 assert timer == null
66             }
67         where: 'the following values are used'
68             scenario   | notificationsEnabled || expectedTimesMethodCalled
69             'enabled'  | true                 || 1
70             'disabled' | false                || 0
71     }
72
73     def 'Exception while sending message.'(){
74         given: 'notifications are enabled'
75             objectUnderTest.notificationsEnabled = true
76         when: 'producer set to throw an exception'
77             mockEventProducer.sendLegacyEvent(*_) >> { throw new KafkaException('sending failed')}
78         and: 'attempt to send events'
79             objectUnderTest.sendLcmEventBatchAsynchronously([cmHandleTransitionPair])
80         then: 'the exception is just logged and not bubbled up'
81             noExceptionThrown()
82         and: 'metrics are not recorded'
83             assert  meterRegistry.find('cps.ncmp.lcm.events.send').timer() == null
84     }
85
86 }