summaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-kafka-consumer/src/test/kotlin/org/onap/dcae/collectors/veshv/kafkaconsumer/impl/OffsetKafkaConsumerTest.kt
blob: faa700bbb86458b0c0645ba376ecb12dc3c79d81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * ============LICENSE_START=======================================================
 * dcaegen2-collectors-veshv
 * ================================================================================
 * Copyright (C) 2019 NOKIA
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.dcae.collectors.veshv.kafkaconsumer.impl

import com.nhaarman.mockitokotlin2.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runBlockingTest
import kotlinx.coroutines.test.setMain
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.TopicPartition
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import org.onap.dcae.collectors.veshv.kafkaconsumer.metrics.Metrics

@ExperimentalCoroutinesApi
object OffsetKafkaConsumerTest : Spek({
    given("OffsetKafkaConsumer") {
        val testDispatcher = TestCoroutineDispatcher()
        val mockedKafkaConsumer = mock<KafkaConsumer<ByteArray, ByteArray>>()
        val mockedMetrics = mock<Metrics>()

        afterEachTest {
            testDispatcher.cleanupTestCoroutines()
            reset(mockedMetrics)
        }

        given("single topicName and partition") {
            val topicName = "topicName"
            val topics = setOf(topicName)
            val offsetKafkaConsumer = OffsetKafkaConsumer(mockedKafkaConsumer, topics, mockedMetrics, testDispatcher)

            on("started OffsetKafkaConsumer") {
                val topicPartition = createTopicPartition(topicName, 0)
                val topicPartitions = setOf(topicPartition)
                whenever(mockedKafkaConsumer.assignment()).thenReturn(topicPartitions)
                whenever(mockedKafkaConsumer.endOffsets(topicPartitions))
                        .thenReturn(mapOf<TopicPartition, Long>(topicPartition to newOffset1))

                runBlockingTest(testDispatcher) {
                    val job = offsetKafkaConsumer.start(updateIntervalInMs)
                    job.cancelAndJoin()
                }

                it("should notify offset changed with $topicName") {
                    verify(mockedMetrics).notifyOffsetChanged(newOffset1, topicPartition)
                }
            }
        }

        given("single topicName and multiple partitions") {
            val topicName = "topicName"
            val topics = setOf(topicName)
            val offsetKafkaConsumer = OffsetKafkaConsumer(mockedKafkaConsumer, topics, mockedMetrics, testDispatcher)

            on("started OffsetKafkaConsumer") {
                val topicPartition1 = createTopicPartition(topicName, 0)
                val topicPartition2 = createTopicPartition(topicName, 2)
                val topicPartition3 = createTopicPartition(topicName, 3)
                val topicPartitions = setOf(topicPartition1, topicPartition2, topicPartition3)
                whenever(mockedKafkaConsumer.assignment()).thenReturn(topicPartitions)
                whenever(mockedKafkaConsumer.endOffsets(topicPartitions))
                        .thenReturn(mapOf<TopicPartition, Long>(
                                topicPartition1 to newOffset1, topicPartition2 to newOffset2, topicPartition3 to newOffset3))

                runBlockingTest(testDispatcher) {
                    val job = offsetKafkaConsumer.start(updateIntervalInMs)
                    job.cancelAndJoin()
                }

                it("should notify offset changed with $topicName") {
                    verify(mockedMetrics).notifyOffsetChanged(newOffset1, topicPartition1)
                    verify(mockedMetrics).notifyOffsetChanged(newOffset2, topicPartition2)
                    verify(mockedMetrics).notifyOffsetChanged(newOffset3, topicPartition3)
                }
            }
        }

        given("two topics with one partition each") {
            val topics = setOf(topicName1, topicName2)
            val kafkaSource = OffsetKafkaConsumer(mockedKafkaConsumer, topics, mockedMetrics, testDispatcher)

            on("started OffsetKafkaConsumer for two iteration of while loop") {
                val offsetArgumentCaptor = argumentCaptor<Long>()
                val topicPartitionArgumentCaptor = argumentCaptor<TopicPartition>()

                val topicPartition1 = createTopicPartition(topicName1, 0)
                val topicPartition2 = createTopicPartition(topicName2, 0)
                val topicPartitions = setOf(topicPartition1, topicPartition2)

                whenever(mockedKafkaConsumer.assignment()).thenReturn(topicPartitions)

                val partitionToOffset1 =
                        mapOf(topicPartition1 to newOffset1,
                                topicPartition2 to newOffset2)
                val partitionToOffset2 =
                        mapOf(topicPartition1 to newOffset2,
                                topicPartition2 to newOffset1)
                whenever(mockedKafkaConsumer.endOffsets(topicPartitions))
                        .thenReturn(partitionToOffset1, partitionToOffset2)

                runBlockingTest(testDispatcher) {
                    val job = kafkaSource.start(updateIntervalInMs)
                    verify(mockedMetrics, times(topicsAmount)).notifyOffsetChanged(
                            offsetArgumentCaptor.capture(),
                            topicPartitionArgumentCaptor.capture()
                    )

                    it("should notify offset changed with proper arguments - before interval"){
                        assertThat(offsetArgumentCaptor.firstValue).isEqualTo(newOffset1)
                        assertThat(topicPartitionArgumentCaptor.firstValue.topic())
                                .isEqualToIgnoringCase(topicPartition1.topic())
                        assertThat(topicPartitionArgumentCaptor.firstValue.partition())
                                .isEqualTo(topicPartition1.partition())

                        assertThat(offsetArgumentCaptor.secondValue).isEqualTo(newOffset2)
                        assertThat(topicPartitionArgumentCaptor.secondValue.topic())
                                .isEqualToIgnoringCase(topicPartition2.topic())
                        assertThat(topicPartitionArgumentCaptor.secondValue.partition())
                                .isEqualTo(topicPartition2.partition())
                    }
                    reset(mockedMetrics)
                    advanceTimeBy(updateIntervalInMs)

                    job.cancelAndJoin()

                    verify(mockedMetrics, times(topicsAmount)).notifyOffsetChanged(
                            offsetArgumentCaptor.capture(),
                            topicPartitionArgumentCaptor.capture()
                    )

                    it("should notify offset changed with proper arguments - after interval") {
                        assertThat(offsetArgumentCaptor.thirdValue).isEqualTo(newOffset2)
                        assertThat(topicPartitionArgumentCaptor.thirdValue.topic())
                                .isEqualToIgnoringCase(topicPartition1.topic())
                        assertThat(topicPartitionArgumentCaptor.thirdValue.partition())
                                .isEqualTo(topicPartition1.partition())

                        assertThat(offsetArgumentCaptor.lastValue).isEqualTo(newOffset1)
                        assertThat(topicPartitionArgumentCaptor.lastValue.topic())
                                .isEqualToIgnoringCase(topicPartition2.topic())
                        assertThat(topicPartitionArgumentCaptor.lastValue.partition())
                                .isEqualTo(topicPartition2.partition())
                    }
                }
            }
        }

        given("empty topicName list") {
            val emptyTopics = emptySet<String>()
            val offsetKafkaConsumer = OffsetKafkaConsumer(mockedKafkaConsumer, emptyTopics, mockedMetrics, testDispatcher)

            on("call of function start") {
                val emptyTopicPartitions = emptySet<TopicPartition>()
                whenever(mockedKafkaConsumer.assignment()).thenReturn(emptyTopicPartitions)
                whenever(mockedKafkaConsumer.endOffsets(emptyTopicPartitions))
                        .thenReturn(emptyMap())

                runBlockingTest(testDispatcher) {
                    val job = offsetKafkaConsumer.start(updateIntervalInMs)
                    job.cancelAndJoin()
                }

                it("should not interact with metrics") {
                    verifyZeroInteractions(mockedMetrics)
                }
            }
        }

    }
})

private const val updateIntervalInMs = 10L
private const val newOffset1 = 2L
private const val newOffset2 = 10L
private const val newOffset3 = 125L
private const val topicName1 = "topicName1"
private const val topicName2 = "topicName2"
private const val topicsAmount = 2
fun createTopicPartition(topic: String, number: Int) = TopicPartition(topic, number)