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.avcsubscription
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.dmi.TestUtils
28 import org.onap.cps.ncmp.dmi.api.kafka.MessagingBaseSpec
29 import org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.CmHandle
30 import org.onap.cps.ncmp.events.avcsubscription1_0_0.ncmp_to_dmi.SubscriptionEvent
31 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.Data
32 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionEventResponse
33 import org.onap.cps.ncmp.events.avcsubscription1_0_0.dmi_to_ncmp.SubscriptionStatus
34 import org.spockframework.spring.SpringBean
35 import org.springframework.boot.test.context.SpringBootTest
36 import org.springframework.test.annotation.DirtiesContext
37 import org.testcontainers.spock.Testcontainers
39 import java.sql.Timestamp
40 import java.time.Duration
41 import java.time.OffsetDateTime
42 import java.time.ZoneId
44 @SpringBootTest(classes = [SubscriptionEventConsumer])
47 class SubscriptionEventConsumerSpec extends MessagingBaseSpec {
49 def objectMapper = new ObjectMapper()
50 def testTopic = 'dmi-ncmp-cm-avc-subscription'
53 SubscriptionEventConsumer objectUnderTest = new SubscriptionEventConsumer(cloudEventKafkaTemplate)
55 def 'Sends subscription cloud event response successfully.'() {
56 given: 'an subscription event response'
57 objectUnderTest.dmiName = 'test-ncmp-dmi'
58 objectUnderTest.cmAvcSubscriptionResponseTopic = testTopic
59 def responseStatus = SubscriptionStatus.Status.ACCEPTED
60 def subscriptionStatuses = [new SubscriptionStatus(id: 'CmHandle1', status: responseStatus),
61 new SubscriptionStatus(id: 'CmHandle2', status: responseStatus)]
62 def subscriptionEventResponseData = new Data(subscriptionName: 'cm-subscription-001',
63 clientId: 'SCO-9989752', dmiName: 'ncmp-dmi-plugin', subscriptionStatus: subscriptionStatuses)
64 def subscriptionEventResponse =
65 new SubscriptionEventResponse().withData(subscriptionEventResponseData)
66 and: 'consumer has a subscription'
67 kafkaConsumer.subscribe([testTopic] as List<String>)
68 when: 'an event is published'
69 def eventKey = UUID.randomUUID().toString()
70 objectUnderTest.sendSubscriptionResponseMessage(eventKey, "subscriptionCreatedStatus", subscriptionEventResponse)
71 and: 'topic is polled'
72 def records = kafkaConsumer.poll(Duration.ofMillis(1500))
73 then: 'poll returns one record'
74 assert records.size() == 1
75 def record = records.iterator().next()
76 and: 'the record value matches the expected event value'
77 def expectedValue = objectMapper.writeValueAsString(subscriptionEventResponse)
78 assert expectedValue == record.value
79 assert eventKey == record.key
82 def 'Consume valid message.'() {
84 objectUnderTest.dmiName = 'test-ncmp-dmi'
85 def eventKey = UUID.randomUUID().toString()
86 def timestamp = new Timestamp(1679521929511)
87 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
88 def subscriptionEvent = objectMapper.readValue(jsonData, SubscriptionEvent.class)
89 objectUnderTest.cmAvcSubscriptionResponseTopic = testTopic
90 def cloudEvent = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSource(URI.create('test-ncmp-dmi'))
91 .withType("subscriptionCreated")
92 .withDataSchema(URI.create("urn:cps:" + SubscriptionEvent.class.getName() + ":1.0.0"))
93 .withExtension("correlationid", eventKey)
94 .withTime(OffsetDateTime.ofInstant(timestamp.toInstant(), ZoneId.of("UTC")))
95 .withData(objectMapper.writeValueAsBytes(subscriptionEvent)).build()
96 def testEventSent = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, eventKey, cloudEvent)
97 when: 'the valid event is consumed'
98 objectUnderTest.consumeSubscriptionEvent(testEventSent)
99 then: 'no exception is thrown'
103 def 'Consume invalid message.'() {
104 given: 'an invalid event type'
105 objectUnderTest.dmiName = 'test-ncmp-dmi'
106 def eventKey = UUID.randomUUID().toString()
107 def timestamp = new Timestamp(1679521929511)
108 objectUnderTest.cmAvcSubscriptionResponseTopic = testTopic
109 def cloudEvent = CloudEventBuilder.v1().withId(UUID.randomUUID().toString()).withSource(URI.create('test-ncmp-dmi'))
110 .withType("subscriptionCreated")
111 .withDataSchema(URI.create("urn:cps:" + SubscriptionEventResponse.class.getName() + ":1.0.0"))
112 .withTime(OffsetDateTime.ofInstant(timestamp.toInstant(), ZoneId.of("UTC")))
113 .withExtension("correlationid", eventKey).build()
114 def testEventSent = new ConsumerRecord<String, CloudEvent>('topic-name', 0, 0, eventKey, cloudEvent)
115 when: 'the invalid event is consumed'
116 objectUnderTest.consumeSubscriptionEvent(testEventSent)
117 then: 'no exception is thrown and event is logged'
121 def 'Form a SubscriptionEventResponse from a SubscriptionEvent.'() {
122 given: 'a SubscriptionEvent'
123 def jsonData = TestUtils.getResourceFileContent('avcSubscriptionCreationEvent.json')
124 def subscriptionEvent = objectMapper.readValue(jsonData, SubscriptionEvent.class)
125 when: 'a SubscriptionResponseEvent is formed'
126 def result = objectUnderTest.formSubscriptionEventResponse(subscriptionEvent)
127 then: 'Confirm SubscriptionEventResponse was formed as expected'
128 assert result.data.clientId == "SCO-9989752"
129 assert result.data.subscriptionName == "cm-subscription-001"
132 def 'Extract cm handle ids from cm handle successfully.'() {
133 given: 'a list of cm handles'
135 [new CmHandle(id:'CmHandle1', additionalProperties: ['prop-x':'prop-valuex']),
136 new CmHandle(id:'CmHandle2', additionalProperties: ['prop-y':'prop-valuey'])]
137 when: 'extract the cm handle ids'
138 def result = objectUnderTest.extractCmHandleIds(cmHandleIds)
139 then: 'cm handle ids are extracted as expected'
140 def expectedCmHandleIds = ['CmHandle1', 'CmHandle2'] as Set
141 assert expectedCmHandleIds == result
144 def 'Populate cm handle id to subscriptionStatus successfully.'() {
145 given: 'a set of cm handle id'
146 def cmHandleIds = ['CmHandle1', 'CmHandle2'] as Set
147 def responseStatus = SubscriptionStatus.Status.ACCEPTED
148 when: 'populate cm handle id to subscriptionStatus'
149 def result = objectUnderTest.populateSubscriptionStatus(cmHandleIds).status
150 then: 'cm handle id to subscriptionStatus populated as expected'
151 def expectedStatus = [responseStatus,responseStatus]
152 expectedStatus == result