aboutsummaryrefslogtreecommitdiffstats
path: root/sources/hv-collector-configuration/src/test/kotlin/org/onap/dcae/collectors/veshv/config/impl/ConfigurationTransformerTest.kt
blob: 42919e4d1e62c4c9654bd81c65555cf2f8a728c6 (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
/*
 * ============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.config.impl

import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.getOrElse
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.fail
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.jetbrains.spek.api.dsl.on
import org.onap.dcae.collectors.veshv.utils.logging.LogLevel
import org.onap.dcaegen2.services.sdk.model.streams.dmaap.KafkaSink
import org.onap.dcaegen2.services.sdk.security.ssl.SecurityKeys
import java.io.File
import java.time.Duration


internal object ConfigurationTransformerTest : Spek({
    describe("ConfigurationTransformer") {
        val cut = ConfigurationTransformer()

        describe("transforming partial configuration to final") {
            val config = ValidatedPartialConfiguration(
                    listenPort = defaultListenPort,
                    idleTimeoutSec = defaultIdleTimeoutSec,
                    cbsConfiguration = ValidatedCbsConfiguration(
                            firstRequestDelaySec = defaultFirstReqDelaySec,
                            requestIntervalSec = defaultRequestIntervalSec
                    ),
                    securityConfiguration = Some(ValidatedSecurityPaths(
                            keyStoreFile = KEYSTORE,
                            keyStorePasswordFile = KEYSTORE_PASS_FILE,
                            trustStoreFile = TRUSTSTORE,
                            trustStorePasswordFile = TRUSTSTORE_PASS_FILE
                    )),
                    streamPublishers = sampleStreamsDefinition,
                    logLevel = Some(LogLevel.TRACE)
            )

            given("transformed configuration") {
                val result = cut.toFinalConfiguration(config)

                it("should create server configuration") {
                    assertThat(result.server.listenPort).isEqualTo(defaultListenPort)
                    assertThat(result.server.idleTimeout)
                            .describedAs("idleTimeout transformed from number to duration")
                            .isEqualTo(Duration.ofSeconds(defaultIdleTimeoutSec))
                }

                it("should create CBS configuration") {
                    assertThat(result.cbs.firstRequestDelay)
                            .describedAs("firstRequestDelay transformed from number to duration")
                            .isEqualTo(Duration.ofSeconds(defaultFirstReqDelaySec))
                    assertThat(result.cbs.requestInterval)
                            .describedAs("requestInterval transformed from number to duration")
                            .isEqualTo(Duration.ofSeconds(defaultRequestIntervalSec))
                }

                it("should create collector configuration") {
                    assertThat(result.collector.routing)
                            .describedAs("routing transformed from kafka sinks to routes")
                            .isEqualTo(sampleRouting)

                    assertThat(result.collector.maxPayloadSizeBytes)
                            .describedAs("maxPayloadSizeBytes calculated from kafka sinks")
                            .isEqualTo(DEFAULT_MAX_PAYLOAD_SIZE_BYTES)
                }

                it("should use specified log level") {
                    assertThat(result.logLevel)
                            .describedAs("logLevel was not transformed when present")
                            .isEqualTo(LogLevel.TRACE)
                }

                it("should create security keys") {
                    result.security.keys.fold({ fail("Should be Some") }, {
                        assertThat(it.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
                        assertThat(it.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
                        it.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
                        it.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
                    })
                }
            }
        }

        describe("transforming configuration with empty log level") {
            val config = validatedConfiguration(
                    logLevel = None
            )

            it("should use default log level") {
                val result = cut.toFinalConfiguration(config)

                assertThat(result.logLevel).isEqualTo(DEFAULT_LOG_LEVEL)
            }
        }

        describe("transforming configuration with security disabled") {
            val config = validatedConfiguration(
                    sslDisable = Some(true),
                    keyStoreFile = "",
                    keyStorePasswordFile = "",
                    trustStoreFile = "",
                    trustStorePasswordFile = ""
            )

            it("should create valid configuration with empty security keys") {
                val result = cut.toFinalConfiguration(config)

                assertThat(result.security.keys).isEqualTo(None)
            }
        }

        describe("transforming configuration with ssl disable missing") {
            val config = validatedConfiguration(
                    sslDisable = None
            )

            it("should create configuration with ssl enabled") {
                val result = cut.toFinalConfiguration(config)
                val securityKeys = result.security.keys
                        .getOrElse { fail("Should be immutableSecurityKeys") } as SecurityKeys
                assertThat(securityKeys.keyStore().path()).isEqualTo(File(KEYSTORE).toPath())
                assertThat(securityKeys.trustStore().path()).isEqualTo(File(TRUSTSTORE).toPath())
                securityKeys.keyStorePassword().use { assertThat(it).isEqualTo(KEYSTORE_PASSWORD.toCharArray()) }
                securityKeys.trustStorePassword().use { assertThat(it).isEqualTo(TRUSTSTORE_PASSWORD.toCharArray()) }
            }
        }

        describe("calculating maxPayloadSizeBytes") {
            on("defined routes") {
                val highestMaxPayloadSize = 3
                val sink1 = mock<KafkaSink>().also {
                    whenever(it.name()).thenReturn("1")
                    whenever(it.maxPayloadSizeBytes()).thenReturn(1)
                }
                val sink2 = mock<KafkaSink>().also {
                    whenever(it.name()).thenReturn("2")
                    whenever(it.maxPayloadSizeBytes()).thenReturn(highestMaxPayloadSize)
                }
                val config = validatedConfiguration(
                        streamPublishers = listOf(sink1, sink2)
                )

                val result = cut.toFinalConfiguration(config)

                it("should use the highest value among all routes") {
                    assertThat(result.collector.maxPayloadSizeBytes)
                            .isEqualTo(highestMaxPayloadSize)
                }
            }

            on("empty routing") {
                val config = validatedConfiguration(
                        streamPublishers = emptyList()
                )

                val result = cut.toFinalConfiguration(config)

                it("should use default value") {
                    assertThat(result.collector.maxPayloadSizeBytes)
                            .isEqualTo(DEFAULT_MAX_PAYLOAD_SIZE_BYTES)
                }
            }
        }

    }
})

private fun validatedConfiguration(listenPort: Int = defaultListenPort,
                                   idleTimeoutSec: Long = defaultIdleTimeoutSec,
                                   firstReqDelaySec: Long = defaultFirstReqDelaySec,
                                   requestIntervalSec: Long = defaultRequestIntervalSec,
                                   sslDisable: Option<Boolean> = Some(false),
                                   keyStoreFile: String = KEYSTORE,
                                   keyStorePasswordFile: String = KEYSTORE_PASS_FILE,
                                   trustStoreFile: String = TRUSTSTORE,
                                   trustStorePasswordFile: String = TRUSTSTORE_PASS_FILE,
                                   streamPublishers: List<KafkaSink> = sampleStreamsDefinition,
                                   logLevel: Option<LogLevel> = Some(LogLevel.INFO)
): ValidatedPartialConfiguration = PartialConfiguration(
        listenPort = Some(listenPort),
        idleTimeoutSec = Some(idleTimeoutSec),
        firstRequestDelaySec = Some(firstReqDelaySec),
        requestIntervalSec = Some(requestIntervalSec),
        streamPublishers = Some(streamPublishers),
        sslDisable = sslDisable,
        keyStoreFile = Some(keyStoreFile),
        keyStorePasswordFile = Some(keyStorePasswordFile),
        trustStoreFile = Some(trustStoreFile),
        trustStorePasswordFile = Some(trustStorePasswordFile),
        logLevel = logLevel
).unsafeAsValidated()