diff options
Diffstat (limited to 'sources/hv-collector-core/src/test/kotlin/org/onap')
5 files changed, 191 insertions, 8 deletions
diff --git a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt index cdee92c9..605e7a6e 100644 --- a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt +++ b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/VesDecoderTest.kt @@ -19,9 +19,7 @@ */ package org.onap.dcae.collectors.veshv.impl -import arrow.core.Option import arrow.core.Try -import arrow.core.success import com.google.protobuf.ByteString import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.dsl.given @@ -32,7 +30,6 @@ import org.onap.dcae.collectors.veshv.domain.VesEventDomain.HEARTBEAT import org.onap.dcae.collectors.veshv.model.VesMessage import org.onap.dcae.collectors.veshv.tests.utils.commonHeader import org.onap.dcae.collectors.veshv.tests.utils.vesEventBytes -import reactor.test.test import java.nio.charset.Charset import kotlin.test.assertTrue import kotlin.test.fail diff --git a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProviderTest.kt b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProviderTest.kt index c6364f74..9ce0c3db 100644 --- a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProviderTest.kt +++ b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/ConsulConfigurationProviderTest.kt @@ -19,6 +19,7 @@ */ package org.onap.dcae.collectors.veshv.impl.adapters +import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.eq import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.whenever @@ -57,7 +58,7 @@ internal object ConsulConfigurationProviderTest : Spek({ val consulConfigProvider = constructConsulConfigProvider(validUrl, httpAdapterMock, healthStateProvider) on("call to consul") { - whenever(httpAdapterMock.get(eq(validUrl), Mockito.anyMap())) + whenever(httpAdapterMock.get(eq(validUrl), any(), Mockito.anyMap())) .thenReturn(Mono.just(constructConsulResponse())) it("should use received configuration") { @@ -97,7 +98,7 @@ internal object ConsulConfigurationProviderTest : Spek({ ) on("call to consul") { - whenever(httpAdapterMock.get(eq(invalidUrl), Mockito.anyMap())) + whenever(httpAdapterMock.get(eq(invalidUrl), any(), Mockito.anyMap())) .thenReturn(Mono.error(RuntimeException("Test exception"))) it("should interrupt the flux") { diff --git a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/HttpAdapterTest.kt b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/HttpAdapterTest.kt index 91457faf..f55b1fdf 100644 --- a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/HttpAdapterTest.kt +++ b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/impl/adapters/HttpAdapterTest.kt @@ -23,11 +23,13 @@ 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.impl.adapters.HttpAdapter.Companion.INVOCATION_ID_HEADER import reactor.core.publisher.Mono import reactor.netty.http.client.HttpClient import reactor.netty.http.server.HttpServer import reactor.test.StepVerifier import reactor.test.test +import java.util.* /** * @author Jakub Dudycz <jakub.dudycz@nokia.com> @@ -42,6 +44,9 @@ internal object HttpAdapterTest : Spek({ routes.get("/url") { req, resp -> resp.sendString(Mono.just(req.uri())) } + routes.get("/inv-id") { req, resp -> + resp.sendString(Mono.just(req.requestHeaders()[INVOCATION_ID_HEADER])) + } } .bindNow() val baseUrl = "http://${httpServer.host()}:${httpServer.port()}" @@ -53,31 +58,46 @@ internal object HttpAdapterTest : Spek({ given("url without query params") { val url = "/url" + val invocationId = UUID.randomUUID() it("should not append query string") { - httpAdapter.get(url).test() + httpAdapter.get(url, invocationId).test() .expectNext(url) .verifyComplete() } + + it("should pass invocation id") { + httpAdapter.get("/inv-id", invocationId).test() + .expectNext(invocationId.toString()) + .verifyComplete() + } } given("url with query params") { val queryParams = mapOf(Pair("p", "the-value")) val url = "/url" + val invocationId = UUID.randomUUID() it("should add them as query string to the url") { - httpAdapter.get(url, queryParams).test() + httpAdapter.get(url, invocationId, queryParams).test() .expectNext("/url?p=the-value") .verifyComplete() } + + it("should pass invocation id") { + httpAdapter.get("/inv-id", invocationId, queryParams).test() + .expectNext(invocationId.toString()) + .verifyComplete() + } } given("invalid url") { val invalidUrl = "/wtf" + val invocationId = UUID.randomUUID() it("should interrupt the flux") { StepVerifier - .create(httpAdapter.get(invalidUrl)) + .create(httpAdapter.get(invalidUrl, invocationId)) .verifyError() } } diff --git a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/model/ClientContextTest.kt b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/model/ClientContextTest.kt new file mode 100644 index 00000000..a49428a7 --- /dev/null +++ b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/model/ClientContextTest.kt @@ -0,0 +1,98 @@ +/* + * ============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.model + +import arrow.core.Some +import com.nhaarman.mockitokotlin2.mock +import com.nhaarman.mockitokotlin2.whenever +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.jetbrains.spek.api.dsl.on +import org.onap.dcae.collectors.veshv.utils.logging.OnapMdc +import java.net.Inet4Address +import java.net.InetAddress +import java.net.InetSocketAddress +import java.security.cert.X509Certificate +import java.util.* +import javax.security.auth.x500.X500Principal + +/** + * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> + * @since December 2018 + */ +internal object ClientContextTest : Spek({ + describe("ClientContext") { + given("default instance") { + val cut = ClientContext() + + on("mapped diagnostic context") { + val mdc = cut.mdc + + it("should contain ${OnapMdc.REQUEST_ID}") { + assertThat(mdc[OnapMdc.REQUEST_ID]).isEqualTo(cut.requestId) + } + + it("should contain ${OnapMdc.INVOCATION_ID}") { + assertThat(mdc[OnapMdc.INVOCATION_ID]).isEqualTo(cut.invocationId) + } + + it("should contain ${OnapMdc.STATUS_CODE}") { + assertThat(mdc[OnapMdc.STATUS_CODE]).isEqualTo("INPROGRESS") + } + + it("should contain ${OnapMdc.CLIENT_NAME}") { + assertThat(mdc[OnapMdc.CLIENT_NAME]).isBlank() + } + + it("should contain ${OnapMdc.CLIENT_IP}") { + assertThat(mdc[OnapMdc.CLIENT_IP]).isBlank() + } + } + } + + given("instance with client data") { + val clientDn = "C=PL, O=Nokia, CN=NokiaBTS" + val clientIp = "192.168.52.34" + val cert: X509Certificate = mock() + val principal: X500Principal = mock() + val cut = ClientContext( + clientAddress = Some(InetAddress.getByName(clientIp)), + clientCert = Some(cert)) + + whenever(cert.subjectX500Principal).thenReturn(principal) + whenever(principal.toString()).thenReturn(clientDn) + + on("mapped diagnostic context") { + val mdc = cut.mdc + + it("should contain ${OnapMdc.CLIENT_NAME}") { + assertThat(mdc[OnapMdc.CLIENT_NAME]).isEqualTo(clientDn) + } + + it("should contain ${OnapMdc.CLIENT_IP}") { + assertThat(mdc[OnapMdc.CLIENT_IP]).isEqualTo(clientIp) + } + } + } + } +}) diff --git a/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/model/ServiceContextTest.kt b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/model/ServiceContextTest.kt new file mode 100644 index 00000000..5b6e4526 --- /dev/null +++ b/sources/hv-collector-core/src/test/kotlin/org/onap/dcae/collectors/veshv/model/ServiceContextTest.kt @@ -0,0 +1,67 @@ +/* + * ============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.model + +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.jetbrains.spek.api.dsl.on +import org.onap.dcae.collectors.veshv.utils.logging.OnapMdc +import java.util.* + +/** + * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com> + * @since December 2018 + */ +internal object ServiceContextTest : Spek({ + describe("ServiceContext") { + given("singleton instance") { + val cut = ServiceContext + + on("instanceId") { + val instanceId = cut.instanceId + it("should be valid UUID") { + UUID.fromString(instanceId) // should not throw + } + } + + on("serverFqdn") { + val serverFqdn = cut.serverFqdn + it("should be non empty") { + assertThat(serverFqdn).isNotBlank() + } + } + + on("mapped diagnostic context") { + val mdc = cut.mdc + + it("should contain ${OnapMdc.INSTANCE_ID}") { + assertThat(mdc[OnapMdc.INSTANCE_ID]).isEqualTo(cut.instanceId) + } + + it("should contain ${OnapMdc.SERVER_FQDN}") { + assertThat(mdc[OnapMdc.SERVER_FQDN]).isEqualTo(cut.serverFqdn) + } + } + } + } +}) |