Creation of server module
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-domain / src / test / kotlin / org / onap / dcae / collectors / veshv / domain / logging / ClientContextTest.kt
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.domain.logging
21
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
34
35 /**
36  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
37  * @since December 2018
38  */
39 internal object ClientContextTest : Spek({
40     describe("ClientContext") {
41         given("default instance") {
42             val cut = ClientContext()
43
44             on("mapped diagnostic context") {
45                 val mdc = cut.mdc
46
47                 it("should contain ${OnapMdc.REQUEST_ID}") {
48                     assertThat(mdc[OnapMdc.REQUEST_ID]).isEqualTo(cut.requestId)
49                 }
50
51                 it("should contain ${OnapMdc.INVOCATION_ID}") {
52                     assertThat(mdc[OnapMdc.INVOCATION_ID]).isEqualTo(cut.invocationId)
53                 }
54
55                 it("should contain ${OnapMdc.STATUS_CODE}") {
56                     assertThat(mdc[OnapMdc.STATUS_CODE]).isEqualTo("INPROGRESS")
57                 }
58
59                 it("should contain ${OnapMdc.CLIENT_NAME}") {
60                     assertThat(mdc[OnapMdc.CLIENT_NAME]).isBlank()
61                 }
62
63                 it("should contain ${OnapMdc.CLIENT_IP}") {
64                     assertThat(mdc[OnapMdc.CLIENT_IP]).isBlank()
65                 }
66             }
67         }
68
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))
77
78             whenever(cert.subjectX500Principal).thenReturn(principal)
79             whenever(principal.toString()).thenReturn(clientDn)
80
81             on("mapped diagnostic context") {
82                 val mdc = cut.mdc
83
84                 it("should contain ${OnapMdc.CLIENT_NAME}") {
85                     assertThat(mdc[OnapMdc.CLIENT_NAME]).isEqualTo(clientDn)
86                 }
87
88                 it("should contain ${OnapMdc.CLIENT_IP}") {
89                     assertThat(mdc[OnapMdc.CLIENT_IP]).isEqualTo(clientIp)
90                 }
91             }
92         }
93     }
94 })