Add metrics for dropped messages
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-ct / src / test / kotlin / org / onap / dcae / collectors / veshv / tests / fakes / metrics.kt
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2018 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.tests.fakes
21
22 import org.onap.dcae.collectors.veshv.boundary.Metrics
23 import org.onap.dcae.collectors.veshv.model.MessageDropCause
24 import java.util.concurrent.ConcurrentHashMap
25 import kotlin.test.fail
26
27 /**
28  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
29  * @since June 2018
30  */
31 class FakeMetrics : Metrics {
32     var bytesReceived: Int = 0
33     var messageBytesReceived: Int = 0
34     var messagesSentCount: Int = 0
35     var messagesDroppedCount: Int = 0
36
37     private val messagesSentToTopic: MutableMap<String, Int> = ConcurrentHashMap()
38     private val messagesDroppedCause: MutableMap<MessageDropCause, Int> = ConcurrentHashMap()
39
40     override fun notifyBytesReceived(size: Int) {
41         bytesReceived += size
42     }
43
44     override fun notifyMessageReceived(size: Int) {
45         messageBytesReceived += size
46     }
47
48     override fun notifyMessageSent(topic: String) {
49         messagesSentCount++
50         messagesSentToTopic.compute(topic) { k, _ -> messagesSentToTopic[k]?.inc() ?: 1 }
51     }
52
53     override fun notifyMessageDropped(cause: MessageDropCause) {
54         messagesDroppedCount++
55         messagesDroppedCause.compute(cause) { k, _ -> messagesDroppedCause[k]?.inc() ?: 1 }
56     }
57
58     fun messagesOnTopic(topic: String) =
59             messagesSentToTopic[topic] ?: fail("No messages were sent to topic $topic")
60
61     fun messagesDropped(cause: MessageDropCause) =
62             messagesDroppedCause[cause]
63                     ?: fail("No messages were dropped due to cause: ${cause.name}")
64 }