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.api.impl.trustlevel
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import com.hazelcast.collection.ISet
25 import io.cloudevents.CloudEvent
26 import io.cloudevents.core.builder.CloudEventBuilder
27 import org.apache.kafka.clients.consumer.ConsumerRecord
28 import org.onap.cps.utils.JsonObjectMapper
29 import org.springframework.boot.test.context.SpringBootTest
30 import spock.lang.Specification
32 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
33 class DeviceHeartbeatConsumerSpec extends Specification {
35 def mockUntrustworthyCmHandlesSet = Mock(ISet<String>)
36 def objectMapper = new ObjectMapper()
38 def objectUnderTest = new DeviceHeartbeatConsumer(mockUntrustworthyCmHandlesSet)
40 def 'Operations to be done in an empty untrustworthy set for #scenario'() {
41 given: 'an event with trustlevel as #trustLevel'
42 def incomingEvent = testCloudEvent(trustLevel)
43 and: 'transformed as a kafka record'
44 def consumerRecord = new ConsumerRecord<String, CloudEvent>('test-device-heartbeat', 0, 0, 'cmhandle1', incomingEvent)
45 consumerRecord.headers().add('ce_id', objectMapper.writeValueAsBytes('cmhandle1'))
46 when: 'the event is consumed'
47 objectUnderTest.heartbeatListener(consumerRecord)
48 then: 'untrustworthy cmhandles are stored'
49 untrustworthyCmHandlesSetInvocationForAdd * mockUntrustworthyCmHandlesSet.add(_)
50 and: 'trustworthy cmHandles will be removed from untrustworthy set'
51 untrustworthyCmHandlesSetInvocationForContains * mockUntrustworthyCmHandlesSet.contains(_)
53 where: 'below scenarios are applicable'
54 scenario | trustLevel || untrustworthyCmHandlesSetInvocationForAdd | untrustworthyCmHandlesSetInvocationForContains
55 'None trust' | TrustLevel.NONE || 1 | 0
56 'Complete trust' | TrustLevel.COMPLETE || 0 | 1
59 def 'Invalid trust'() {
60 when: 'we provide an invalid trust in the event'
61 def consumerRecord = new ConsumerRecord<String, CloudEvent>('test-device-heartbeat', 0, 0, 'cmhandle1', testCloudEvent(null))
62 consumerRecord.headers().add('ce_id', objectMapper.writeValueAsBytes('cmhandle1'))
63 objectUnderTest.heartbeatListener(consumerRecord)
64 then: 'no interaction with the untrustworthy cmhandles set'
65 0 * mockUntrustworthyCmHandlesSet.add(_)
66 0 * mockUntrustworthyCmHandlesSet.contains(_)
67 0 * mockUntrustworthyCmHandlesSet.remove(_)
68 and: 'control flow returns without any exception'
73 def 'Remove trustworthy cmhandles from untrustworthy cmhandles set'() {
74 given: 'an event with COMPLETE trustlevel'
75 def incomingEvent = testCloudEvent(TrustLevel.COMPLETE)
76 and: 'transformed as a kafka record'
77 def consumerRecord = new ConsumerRecord<String, CloudEvent>('test-device-heartbeat', 0, 0, 'cmhandle1', incomingEvent)
78 consumerRecord.headers().add('ce_id', objectMapper.writeValueAsBytes('cmhandle1'))
79 and: 'untrustworthy cmhandles set contains cmhandle1'
80 1 * mockUntrustworthyCmHandlesSet.contains(_) >> true
81 when: 'the event is consumed'
82 objectUnderTest.heartbeatListener(consumerRecord)
83 then: 'cmhandle removed from untrustworthy cmhandles set'
84 1 * mockUntrustworthyCmHandlesSet.remove(_) >> {
87 args[0].equals('cmhandle1')
93 def testCloudEvent(trustLevel) {
94 return CloudEventBuilder.v1().withData(objectMapper.writeValueAsBytes(new DeviceTrustLevel(trustLevel)))
96 .withSource(URI.create('DMI'))
97 .withDataSchema(URI.create('test'))
98 .withType('org.onap.cm.events.trustlevel-notification')