Add metrics for NCMP passthrough read operation
[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.map.IMap
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.ncmp.events.trustlevel.DeviceTrustLevel
29 import org.onap.cps.utils.JsonObjectMapper
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.test.context.SpringBootTest
32 import spock.lang.Specification
33
34 @SpringBootTest(classes = [ObjectMapper, JsonObjectMapper])
35 class DeviceHeartbeatConsumerSpec extends Specification {
36
37     def mockTrustLevelPerCmHandle = Mock(Map<String, TrustLevel>)
38
39     def objectUnderTest = new DeviceHeartbeatConsumer(mockTrustLevelPerCmHandle)
40     def objectMapper = new ObjectMapper()
41
42     @Autowired
43     JsonObjectMapper jsonObjectMapper
44
45     def static trustLevelString = '{"data":{"trustLevel": "COMPLETE"}}'
46
47     def 'Consume a trustlevel event'() {
48         given: 'an event from dmi with trust level complete'
49             def payload = jsonObjectMapper.convertJsonString(trustLevelString, DeviceTrustLevel.class)
50             def eventFromDmi = createTrustLevelEvent(payload)
51         and: 'transformed to a consumer record with a cloud event id (ce_id)'
52             def consumerRecord = new ConsumerRecord<String, CloudEvent>('test-device-heartbeat', 0, 0, 'sample-message-key', eventFromDmi)
53             consumerRecord.headers().add('ce_id', objectMapper.writeValueAsBytes('cmhandle1'))
54         when: 'the event is consumed'
55             objectUnderTest.heartbeatListener(consumerRecord)
56         then: 'cm handles are stored with correct trust level'
57             1 * mockTrustLevelPerCmHandle.put('"cmhandle1"', TrustLevel.COMPLETE)
58     }
59
60     def 'Consume trustlevel event without cloud event id'() {
61         given: 'an event from dmi'
62             def payload = jsonObjectMapper.convertJsonString(trustLevelString, DeviceTrustLevel.class)
63             def eventFromDmi = createTrustLevelEvent(payload)
64         and: 'transformed to a consumer record WITHOUT Cloud event ID (ce_id)'
65             def consumerRecord = new ConsumerRecord<String, CloudEvent>('test-device-heartbeat', 0, 0, 'sample-message-key', eventFromDmi)
66         when: 'the event is consumed'
67             objectUnderTest.heartbeatListener(consumerRecord)
68         then: 'no cm handle has been stored in the map'
69             0 * mockTrustLevelPerCmHandle.put(*_)
70     }
71
72     def 'Consume a trust level event without payload'() {
73         given: 'a consumer record with ce_id header but without payload'
74             def consumerRecord = new ConsumerRecord<String, CloudEvent>('test-device-heartbeat', 0, 0, 'cmhandle1', createTrustLevelEvent(null))
75             consumerRecord.headers().add('some_other_header_value', objectMapper.writeValueAsBytes('cmhandle1'))
76         when: 'the event is consumed'
77             objectUnderTest.heartbeatListener(consumerRecord)
78         then: 'no cm handle has been stored in the map'
79             0 * mockTrustLevelPerCmHandle.put(*_)
80     }
81
82     def createTrustLevelEvent(eventPayload) {
83         return CloudEventBuilder.v1().withData(objectMapper.writeValueAsBytes(eventPayload))
84             .withId("cmhandle1")
85             .withSource(URI.create('DMI'))
86             .withDataSchema(URI.create('test'))
87             .withType('org.onap.cps.ncmp.events.trustlevel.DeviceTrustLevel')
88             .build()
89     }
90
91 }