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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.dcae.collectors.veshv.domain.logging
22 import arrow.core.Some
23 import com.nhaarman.mockitokotlin2.mock
24 import com.nhaarman.mockitokotlin2.whenever
25 import org.assertj.core.api.Assertions.assertThat
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.given
29 import org.jetbrains.spek.api.dsl.it
30 import org.jetbrains.spek.api.dsl.on
31 import java.net.InetAddress
32 import java.security.cert.X509Certificate
33 import javax.security.auth.x500.X500Principal
36 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
37 * @since December 2018
39 internal object ClientContextTest : Spek({
40 describe("ClientContext") {
41 given("default instance") {
42 val cut = ClientContext()
44 on("mapped diagnostic context") {
47 it("should contain ${OnapMdc.REQUEST_ID}") {
48 assertThat(mdc[OnapMdc.REQUEST_ID]).isEqualTo(cut.requestId)
51 it("should contain ${OnapMdc.INVOCATION_ID}") {
52 assertThat(mdc[OnapMdc.INVOCATION_ID]).isEqualTo(cut.invocationId)
55 it("should contain ${OnapMdc.STATUS_CODE}") {
56 assertThat(mdc[OnapMdc.STATUS_CODE]).isEqualTo("INPROGRESS")
59 it("should contain ${OnapMdc.CLIENT_NAME}") {
60 assertThat(mdc[OnapMdc.CLIENT_NAME]).isBlank()
63 it("should contain ${OnapMdc.CLIENT_IP}") {
64 assertThat(mdc[OnapMdc.CLIENT_IP]).isBlank()
69 given("instance with client data") {
70 val clientDn = "C=PL, O=Nokia, CN=NokiaBTS"
71 val clientIp = "192.168.52.34"
72 val cert: X509Certificate = mock()
73 val principal: X500Principal = mock()
74 val cut = ClientContext(
75 clientAddress = Some(InetAddress.getByName(clientIp)),
76 clientCert = Some(cert))
78 whenever(cert.subjectX500Principal).thenReturn(principal)
79 whenever(principal.toString()).thenReturn(clientDn)
81 on("mapped diagnostic context") {
84 it("should contain ${OnapMdc.CLIENT_NAME}") {
85 assertThat(mdc[OnapMdc.CLIENT_NAME]).isEqualTo(clientDn)
88 it("should contain ${OnapMdc.CLIENT_IP}") {
89 assertThat(mdc[OnapMdc.CLIENT_IP]).isEqualTo(clientIp)