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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.events.lcm
23 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
24 import org.onap.cps.ncmp.events.lcm.v1.LcmEvent
25 import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader
26 import org.onap.cps.utils.JsonObjectMapper
27 import org.springframework.kafka.KafkaException
28 import spock.lang.Specification
30 class LcmEventsServiceSpec extends Specification {
32 def mockLcmEventsPublisher = Mock(EventsPublisher)
33 def mockJsonObjectMapper = Mock(JsonObjectMapper)
35 def objectUnderTest = new LcmEventsService(mockLcmEventsPublisher, mockJsonObjectMapper)
37 def 'Create and Publish lcm event where events are #scenario'() {
38 given: 'a cm handle id, Lcm Event, and headers'
39 def cmHandleId = 'test-cm-handle-id'
40 def eventId = UUID.randomUUID().toString()
41 def lcmEvent = new LcmEvent(eventId: eventId, eventCorrelationId: cmHandleId)
42 and: 'we also have a lcm event header'
43 def lcmEventHeader = new LcmEventHeader(eventId: eventId, eventCorrelationId: cmHandleId)
44 and: 'notificationsEnabled is #notificationsEnabled and it will be true as default'
45 objectUnderTest.notificationsEnabled = notificationsEnabled
46 and: 'lcm event header is transformed to headers map'
47 mockJsonObjectMapper.convertToValueType(lcmEventHeader, Map.class) >> ['eventId': eventId, 'eventCorrelationId': cmHandleId]
48 when: 'service is called to publish lcm event'
49 objectUnderTest.publishLcmEvent('test-cm-handle-id', lcmEvent, lcmEventHeader)
50 then: 'publisher is called #expectedTimesMethodCalled times'
51 expectedTimesMethodCalled * mockLcmEventsPublisher.publishEvent(_, cmHandleId, _, lcmEvent) >> {
53 def eventHeaders = (args[2] as Map<String,Object>)
54 assert eventHeaders.containsKey('eventId')
55 assert eventHeaders.containsKey('eventCorrelationId')
56 assert eventHeaders.get('eventId') == eventId
57 assert eventHeaders.get('eventCorrelationId') == cmHandleId
60 where: 'the following values are used'
61 scenario | notificationsEnabled || expectedTimesMethodCalled
63 'disabled' | false || 0
66 def 'Unable to send message'(){
67 given: 'a cm handle id and Lcm Event and notification enabled'
68 def cmHandleId = 'test-cm-handle-id'
69 def eventId = UUID.randomUUID().toString()
70 def lcmEvent = new LcmEvent(eventId: eventId, eventCorrelationId: cmHandleId)
71 def lcmEventHeader = new LcmEventHeader(eventId: eventId, eventCorrelationId: cmHandleId)
72 objectUnderTest.notificationsEnabled = true
73 when: 'publisher set to throw an exception'
74 mockLcmEventsPublisher.publishEvent(_, _, _, _) >> { throw new KafkaException('publishing failed')}
75 and: 'an event is publised'
76 objectUnderTest.publishLcmEvent(cmHandleId, lcmEvent, lcmEventHeader)
77 then: 'the exception is just logged and not bubbled up'