aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-ct/src/test/kotlin/org/onap/dcae/collectors/veshv/tests/component/VesHvSpecification.kt
blob: 21c5c189d6f1978708fe1e7c2cf6eedc0cbecba1 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * ============LICENSE_START=======================================================
 * dcaegen2-collectors-veshv
 * ================================================================================
 * Copyright (C) 2018 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.tests.component

import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.onap.dcae.collectors.veshv.domain.VesEventDomain.HEARTBEAT
import org.onap.dcae.collectors.veshv.domain.VesEventDomain.MEASUREMENT
import org.onap.dcae.collectors.veshv.domain.VesEventDomain.OTHER
import org.onap.dcae.collectors.veshv.domain.VesEventDomain.PERF3GPP
import org.onap.dcae.collectors.veshv.healthcheck.api.HealthDescription
import org.onap.dcae.collectors.veshv.tests.fakes.ALTERNATE_PERF3GPP_TOPIC
import org.onap.dcae.collectors.veshv.tests.fakes.MEASUREMENTS_FOR_VF_SCALING_TOPIC
import org.onap.dcae.collectors.veshv.tests.fakes.PERF3GPP_TOPIC
import org.onap.dcae.collectors.veshv.tests.fakes.StoringSink
import org.onap.dcae.collectors.veshv.tests.fakes.configWithBasicRouting
import org.onap.dcae.collectors.veshv.tests.fakes.configWithDifferentRouting
import org.onap.dcae.collectors.veshv.tests.fakes.configWithEmptyRouting
import org.onap.dcae.collectors.veshv.tests.fakes.configWithTwoDomainsToOneTopicRouting
import org.onap.dcae.collectors.veshv.tests.utils.garbageFrame
import org.onap.dcae.collectors.veshv.tests.utils.messageWithInvalidWireFrameHeader
import org.onap.dcae.collectors.veshv.tests.utils.messageWithPayloadOfSize
import org.onap.dcae.collectors.veshv.tests.utils.vesWireFrameMessage
import org.onap.dcae.collectors.veshv.tests.utils.wireFrameMessageWithInvalidPayload
import reactor.core.publisher.Flux
import java.time.Duration

/**
 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
 * @since May 2018
 */
object VesHvSpecification : Spek({
    debugRx(false)

    describe("VES High Volume Collector") {
        it("should handle multiple HV RAN events") {
            val (sut, sink) = vesHvWithStoringSink()
            val messages = sut.handleConnection(sink,
                    vesWireFrameMessage(PERF3GPP),
                    vesWireFrameMessage(PERF3GPP)
            )

            assertThat(messages)
                    .describedAs("should send all events")
                    .hasSize(2)
        }

        it("should close sink when closing collector provider") {
            val (sut, _) = vesHvWithStoringSink()

            sut.close()

            assertThat(sut.sinkProvider.closed).isTrue()
        }
    }

    describe("Memory management") {
        it("should release memory for each handled and dropped message") {
            val (sut, sink) = vesHvWithStoringSink()
            val validMessage = vesWireFrameMessage(PERF3GPP)
            val msgWithInvalidFrame = messageWithInvalidWireFrameHeader()
            val msgWithTooBigPayload = messageWithPayloadOfSize(Sut.MAX_PAYLOAD_SIZE_BYTES + 1, PERF3GPP)
            val expectedRefCnt = 0

            val handledEvents = sut.handleConnection(
                    sink, validMessage, msgWithInvalidFrame, msgWithTooBigPayload)

            assertThat(handledEvents).hasSize(1)

            assertThat(validMessage.refCnt())
                    .describedAs("handled message should be released")
                    .isEqualTo(expectedRefCnt)
            assertThat(msgWithInvalidFrame.refCnt())
                    .describedAs("message with invalid frame should be released")
                    .isEqualTo(expectedRefCnt)
            assertThat(msgWithTooBigPayload.refCnt())
                    .describedAs("message with payload exceeding 1MiB should be released")
                    .isEqualTo(expectedRefCnt)
        }

        it("should release memory for each message with invalid payload") {
            val (sut, sink) = vesHvWithStoringSink()
            val validMessage = vesWireFrameMessage(PERF3GPP)
            val msgWithInvalidPayload = wireFrameMessageWithInvalidPayload()
            val expectedRefCnt = 0

            val handledEvents = sut.handleConnection(sink, validMessage, msgWithInvalidPayload)

            assertThat(handledEvents).hasSize(1)

            assertThat(validMessage.refCnt())
                    .describedAs("handled message should be released")
                    .isEqualTo(expectedRefCnt)
            assertThat(msgWithInvalidPayload.refCnt())
                    .describedAs("message with invalid payload should be released")
                    .isEqualTo(expectedRefCnt)

        }

        it("should release memory for each message with garbage frame") {
            val (sut, sink) = vesHvWithStoringSink()
            val validMessage = vesWireFrameMessage(PERF3GPP)
            val msgWithGarbageFrame = garbageFrame()
            val expectedRefCnt = 0

            val handledEvents = sut.handleConnection(sink, validMessage, msgWithGarbageFrame)

            assertThat(handledEvents).hasSize(1)

            assertThat(validMessage.refCnt())
                    .describedAs("handled message should be released")
                    .isEqualTo(expectedRefCnt)
            assertThat(msgWithGarbageFrame.refCnt())
                    .describedAs("message with garbage frame should be released")
                    .isEqualTo(expectedRefCnt)

        }
    }

    describe("message routing") {
        it("should direct message to a topic by means of routing configuration") {
            val (sut, sink) = vesHvWithStoringSink()

            val messages = sut.handleConnection(sink, vesWireFrameMessage(PERF3GPP))
            assertThat(messages).describedAs("number of routed messages").hasSize(1)

            val msg = messages[0]
            assertThat(msg.topic).describedAs("routed message topic").isEqualTo(PERF3GPP_TOPIC)
            assertThat(msg.partition).describedAs("routed message partition").isEqualTo(0)
        }

        it("should be able to direct 2 messages from different domains to one topic") {
            val (sut, sink) = vesHvWithStoringSink()

            sut.configurationProvider.updateConfiguration(configWithTwoDomainsToOneTopicRouting)

            val messages = sut.handleConnection(sink,
                    vesWireFrameMessage(PERF3GPP),
                    vesWireFrameMessage(HEARTBEAT),
                    vesWireFrameMessage(MEASUREMENT))

            assertThat(messages).describedAs("number of routed messages").hasSize(3)

            assertThat(messages[0].topic).describedAs("first message topic")
                    .isEqualTo(PERF3GPP_TOPIC)

            assertThat(messages[1].topic).describedAs("second message topic")
                    .isEqualTo(PERF3GPP_TOPIC)

            assertThat(messages[2].topic).describedAs("last message topic")
                    .isEqualTo(MEASUREMENTS_FOR_VF_SCALING_TOPIC)
        }

        it("should drop message if route was not found") {
            val (sut, sink) = vesHvWithStoringSink()
            val messages = sut.handleConnection(sink,
                    vesWireFrameMessage(OTHER, "first"),
                    vesWireFrameMessage(PERF3GPP, "second"),
                    vesWireFrameMessage(HEARTBEAT, "third"))

            assertThat(messages).describedAs("number of routed messages").hasSize(1)

            val msg = messages[0]
            assertThat(msg.topic).describedAs("routed message topic").isEqualTo(PERF3GPP_TOPIC)
            assertThat(msg.message.header.eventId).describedAs("routed message eventId").isEqualTo("second")
        }
    }

    describe("configuration update") {

        val defaultTimeout = Duration.ofSeconds(10)

        given("successful configuration change") {

            lateinit var sut: Sut
            lateinit var sink: StoringSink

            beforeEachTest {
                vesHvWithStoringSink().run {
                    sut = first
                    sink = second
                }
            }

            it("should update collector") {
                val firstCollector = sut.collector

                sut.configurationProvider.updateConfiguration(configWithDifferentRouting)
                val collectorAfterUpdate = sut.collector

                assertThat(collectorAfterUpdate).isNotSameAs(firstCollector)
            }

            it("should start routing messages") {

                sut.configurationProvider.updateConfiguration(configWithEmptyRouting)

                val messages = sut.handleConnection(sink, vesWireFrameMessage(PERF3GPP))
                assertThat(messages).isEmpty()

                sut.configurationProvider.updateConfiguration(configWithBasicRouting)

                val messagesAfterUpdate = sut.handleConnection(sink, vesWireFrameMessage(PERF3GPP))
                assertThat(messagesAfterUpdate).hasSize(1)
                val message = messagesAfterUpdate[0]

                assertThat(message.topic).describedAs("routed message topic after configuration's change")
                        .isEqualTo(PERF3GPP_TOPIC)
                assertThat(message.partition).describedAs("routed message partition")
                        .isEqualTo(0)
            }

            it("should change domain routing") {

                val messages = sut.handleConnection(sink, vesWireFrameMessage(PERF3GPP))
                assertThat(messages).hasSize(1)
                val firstMessage = messages[0]

                assertThat(firstMessage.topic).describedAs("routed message topic on initial configuration")
                        .isEqualTo(PERF3GPP_TOPIC)
                assertThat(firstMessage.partition).describedAs("routed message partition")
                        .isEqualTo(0)


                sut.configurationProvider.updateConfiguration(configWithDifferentRouting)

                val messagesAfterUpdate = sut.handleConnection(sink, vesWireFrameMessage(PERF3GPP))
                assertThat(messagesAfterUpdate).hasSize(2)
                val secondMessage = messagesAfterUpdate[1]

                assertThat(secondMessage.topic).describedAs("routed message topic after configuration's change")
                        .isEqualTo(ALTERNATE_PERF3GPP_TOPIC)
                assertThat(secondMessage.partition).describedAs("routed message partition")
                        .isEqualTo(0)
            }

            it("should update routing for each client sending one message") {

                val messagesAmount = 10
                val messagesForEachTopic = 5

                Flux.range(0, messagesAmount).doOnNext {
                    if (it == messagesForEachTopic) {
                        sut.configurationProvider.updateConfiguration(configWithDifferentRouting)
                    }
                }.doOnNext {
                    sut.handleConnection(sink, vesWireFrameMessage(PERF3GPP))
                }.then().block(defaultTimeout)


                val messages = sink.sentMessages
                val firstTopicMessagesCount = messages.count { it.topic == PERF3GPP_TOPIC }
                val secondTopicMessagesCount = messages.count { it.topic == ALTERNATE_PERF3GPP_TOPIC }

                assertThat(messages.size).isEqualTo(messagesAmount)
                assertThat(messagesForEachTopic)
                        .describedAs("amount of messages routed to each topic")
                        .isEqualTo(firstTopicMessagesCount)
                        .isEqualTo(secondTopicMessagesCount)
            }

            it("should not update routing for client sending continuous stream of messages") {

                val messageStreamSize = 10
                val pivot = 5

                val incomingMessages = Flux.range(0, messageStreamSize)
                        .doOnNext {
                            if (it == pivot) {
                                sut.configurationProvider.updateConfiguration(configWithDifferentRouting)
                                println("config changed")
                            }
                        }
                        .map { vesWireFrameMessage(PERF3GPP) }


                sut.collector.handleConnection(incomingMessages).block(defaultTimeout)

                val messages = sink.sentMessages
                val firstTopicMessagesCount = messages.count { it.topic == PERF3GPP_TOPIC }
                val secondTopicMessagesCount = messages.count { it.topic == ALTERNATE_PERF3GPP_TOPIC }

                assertThat(messages.size).isEqualTo(messageStreamSize)
                assertThat(firstTopicMessagesCount)
                        .describedAs("amount of messages routed to first topic")
                        .isEqualTo(messageStreamSize)

                assertThat(secondTopicMessagesCount)
                        .describedAs("amount of messages routed to second topic")
                        .isEqualTo(0)
            }

            it("should mark the application healthy") {
                assertThat(sut.healthStateProvider.currentHealth)
                        .describedAs("application health state")
                        .isEqualTo(HealthDescription.HEALTHY)
            }
        }

        given("failed configuration change") {
            val (sut, _) = vesHvWithStoringSink()
            sut.configurationProvider.shouldThrowExceptionOnConfigUpdate(true)
            sut.configurationProvider.updateConfiguration(configWithBasicRouting)

            it("should mark the application unhealthy ") {
                assertThat(sut.healthStateProvider.currentHealth)
                        .describedAs("application health state")
                        .isEqualTo(HealthDescription.DYNAMIC_CONFIGURATION_NOT_FOUND)
            }
        }
    }

    describe("request validation") {
        it("should reject message with payload greater than 1 MiB and all subsequent messages") {
            val (sut, sink) = vesHvWithStoringSink()

            val handledMessages = sut.handleConnection(sink,
                    vesWireFrameMessage(PERF3GPP, "first"),
                    messageWithPayloadOfSize(Sut.MAX_PAYLOAD_SIZE_BYTES + 1, PERF3GPP),
                    vesWireFrameMessage(PERF3GPP))

            assertThat(handledMessages).hasSize(1)
            assertThat(handledMessages.first().message.header.eventId).isEqualTo("first")
        }
    }

})

private fun vesHvWithStoringSink(): Pair<Sut, StoringSink> {
    val sink = StoringSink()
    val sut = Sut(sink)
    sut.configurationProvider.updateConfiguration(configWithBasicRouting)
    return Pair(sut, sink)
}