2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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.dmi.notifications.avc
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.apache.kafka.clients.consumer.KafkaConsumer
25 import org.apache.kafka.common.serialization.StringDeserializer
26 import org.onap.cps.ncmp.dmi.TestUtils
27 import org.onap.cps.ncmp.dmi.api.kafka.MessagingBaseSpec
28 import org.onap.cps.ncmp.dmi.service.model.SubscriptionEventResponse
29 import org.onap.cps.ncmp.dmi.service.model.SubscriptionEventResponseStatus
30 import org.onap.cps.ncmp.event.model.SubscriptionEvent
31 import org.spockframework.spring.SpringBean
32 import org.springframework.boot.test.context.SpringBootTest
33 import org.springframework.test.annotation.DirtiesContext
34 import org.testcontainers.spock.Testcontainers
36 import java.time.Duration
38 @SpringBootTest(classes = [SubscriptionEventConsumer])
41 class SubscriptionEventConsumerSpec extends MessagingBaseSpec {
43 def objectMapper = new ObjectMapper()
44 def testTopic = 'dmi-ncmp-cm-avc-subscription'
47 SubscriptionEventConsumer objectUnderTest = new SubscriptionEventConsumer(kafkaTemplate)
49 def 'Sends subscription event response successfully.'() {
50 given: 'an subscription event response'
51 def responseStatus = SubscriptionEventResponseStatus.ACCEPTED
52 def cmHandleIdToStatusMap = ['CmHandle1':responseStatus, 'CmHandle2':responseStatus]
53 def subscriptionEventResponse = new SubscriptionEventResponse(subscriptionName: 'cm-subscription-001',
54 clientId: 'SCO-9989752', dmiName: 'ncmp-dmi-plugin', cmHandleIdToStatus: cmHandleIdToStatusMap)
55 objectUnderTest.cmAvcSubscriptionResponseTopic = testTopic
56 and: 'consumer has a subscription'
57 kafkaConsumer.subscribe([testTopic] as List<String>)
58 when: 'an event is published'
59 def eventKey = UUID.randomUUID().toString()
60 objectUnderTest.sendSubscriptionResponseMessage(eventKey, subscriptionEventResponse)
61 and: 'topic is polled'
62 def records = kafkaConsumer.poll(Duration.ofMillis(1500))
63 then: 'poll returns one record'
64 assert records.size() == 1
65 def record = records.iterator().next()
66 and: 'the record value matches the expected event value'
67 def expectedValue = objectMapper.writeValueAsString(subscriptionEventResponse)
68 assert expectedValue == record.value
69 assert eventKey == record.key
72 def 'Consume valid message.'() {
74 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
75 def testEventSent = objectMapper.readValue(jsonData, SubscriptionEvent.class)
76 objectUnderTest.cmAvcSubscriptionResponseTopic = testTopic
77 when: 'the valid event is consumed'
78 def eventKey = UUID.randomUUID().toString()
79 def timeStampReceived = '1679521929511'
80 objectUnderTest.consumeSubscriptionEvent(testEventSent, eventKey, timeStampReceived)
81 then: 'no exception is thrown'
85 def 'Extract cm handle ids from cm handle id to cm handle property map successfully.'() {
86 given: 'a list of cm handle id to cm handle property map'
87 def cmHandleIdToPropertyMap =
88 ['CmHandle1':['prop-x':'prop-valuex'], 'CmHandle2':['prop-y':'prop-valuey']]
89 def listOfCmHandleIdToPropertyMap =
90 [cmHandleIdToPropertyMap]
91 when: 'extract the cm handle ids'
92 def result = objectUnderTest.extractCmHandleIds(listOfCmHandleIdToPropertyMap)
93 then: 'cm handle ids are extracted as expected'
94 def expectedCmHandleIds = ['CmHandle1', 'CmHandle2'] as Set
95 assert expectedCmHandleIds == result
98 def 'Populate cm handle id to status map successfully.'() {
99 given: 'a set of cm handle id'
100 def cmHandleIds = ['CmHandle1', 'CmHandle2'] as Set
101 def responseStatus = SubscriptionEventResponseStatus.ACCEPTED
102 when: 'populate cm handle id to status map'
103 def result = objectUnderTest.populateCmHandleIdToStatus(cmHandleIds)
104 then: 'cm handle id to status map populated as expected'
105 def expectedMap = ['CmHandle1':responseStatus,'CmHandle2':responseStatus]
106 expectedMap == result