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.model
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 org.onap.dcae.collectors.veshv.utils.logging.OnapMdc
32 import java.net.Inet4Address
33 import java.net.InetAddress
34 import java.net.InetSocketAddress
35 import java.security.cert.X509Certificate
37 import javax.security.auth.x500.X500Principal
40 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
41 * @since December 2018
43 internal object ClientContextTest : Spek({
44 describe("ClientContext") {
45 given("default instance") {
46 val cut = ClientContext()
48 on("mapped diagnostic context") {
51 it("should contain ${OnapMdc.REQUEST_ID}") {
52 assertThat(mdc[OnapMdc.REQUEST_ID]).isEqualTo(cut.requestId)
55 it("should contain ${OnapMdc.INVOCATION_ID}") {
56 assertThat(mdc[OnapMdc.INVOCATION_ID]).isEqualTo(cut.invocationId)
59 it("should contain ${OnapMdc.STATUS_CODE}") {
60 assertThat(mdc[OnapMdc.STATUS_CODE]).isEqualTo("INPROGRESS")
63 it("should contain ${OnapMdc.CLIENT_NAME}") {
64 assertThat(mdc[OnapMdc.CLIENT_NAME]).isBlank()
67 it("should contain ${OnapMdc.CLIENT_IP}") {
68 assertThat(mdc[OnapMdc.CLIENT_IP]).isBlank()
73 given("instance with client data") {
74 val clientDn = "C=PL, O=Nokia, CN=NokiaBTS"
75 val clientIp = "192.168.52.34"
76 val cert: X509Certificate = mock()
77 val principal: X500Principal = mock()
78 val cut = ClientContext(
79 clientAddress = Some(InetAddress.getByName(clientIp)),
80 clientCert = Some(cert))
82 whenever(cert.subjectX500Principal).thenReturn(principal)
83 whenever(principal.toString()).thenReturn(clientDn)
85 on("mapped diagnostic context") {
88 it("should contain ${OnapMdc.CLIENT_NAME}") {
89 assertThat(mdc[OnapMdc.CLIENT_NAME]).isEqualTo(clientDn)
92 it("should contain ${OnapMdc.CLIENT_IP}") {
93 assertThat(mdc[OnapMdc.CLIENT_IP]).isEqualTo(clientIp)