Add NCMP Stub documentation to RTD
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / trustlevel / DeviceHeartbeatConsumerSpec.groovy
1 /*
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
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.api.impl.trustlevel
22
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
31
32 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
33 class DeviceHeartbeatConsumerSpec extends Specification {
34
35     def mockUntrustworthyCmHandlesSet = Mock(ISet<String>)
36     def objectMapper = new ObjectMapper()
37
38     def objectUnderTest = new DeviceHeartbeatConsumer(mockUntrustworthyCmHandlesSet)
39
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(_)
52
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
57     }
58
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'
69             noExceptionThrown()
70
71     }
72
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(_) >> {
85                 args ->
86                     {
87                         args[0].equals('cmhandle1')
88                     }
89             }
90
91     }
92
93     def testCloudEvent(trustLevel) {
94         return CloudEventBuilder.v1().withData(objectMapper.writeValueAsBytes(new DeviceTrustLevel(trustLevel)))
95             .withId("cmhandle1")
96             .withSource(URI.create('DMI'))
97             .withDataSchema(URI.create('test'))
98             .withType('org.onap.cm.events.trustlevel-notification')
99             .build()
100     }
101
102 }