Merge "Extract transforming logic from validator"
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-core / src / test / kotlin / org / onap / dcae / collectors / veshv / model / 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.model
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 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
36 import java.util.*
37 import javax.security.auth.x500.X500Principal
38
39 /**
40  * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
41  * @since December 2018
42  */
43 internal object ClientContextTest : Spek({
44     describe("ClientContext") {
45         given("default instance") {
46             val cut = ClientContext()
47
48             on("mapped diagnostic context") {
49                 val mdc = cut.mdc
50
51                 it("should contain ${OnapMdc.REQUEST_ID}") {
52                     assertThat(mdc[OnapMdc.REQUEST_ID]).isEqualTo(cut.requestId)
53                 }
54
55                 it("should contain ${OnapMdc.INVOCATION_ID}") {
56                     assertThat(mdc[OnapMdc.INVOCATION_ID]).isEqualTo(cut.invocationId)
57                 }
58
59                 it("should contain ${OnapMdc.STATUS_CODE}") {
60                     assertThat(mdc[OnapMdc.STATUS_CODE]).isEqualTo("INPROGRESS")
61                 }
62
63                 it("should contain ${OnapMdc.CLIENT_NAME}") {
64                     assertThat(mdc[OnapMdc.CLIENT_NAME]).isBlank()
65                 }
66
67                 it("should contain ${OnapMdc.CLIENT_IP}") {
68                     assertThat(mdc[OnapMdc.CLIENT_IP]).isBlank()
69                 }
70             }
71         }
72
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))
81
82             whenever(cert.subjectX500Principal).thenReturn(principal)
83             whenever(principal.toString()).thenReturn(clientDn)
84
85             on("mapped diagnostic context") {
86                 val mdc = cut.mdc
87
88                 it("should contain ${OnapMdc.CLIENT_NAME}") {
89                     assertThat(mdc[OnapMdc.CLIENT_NAME]).isEqualTo(clientDn)
90                 }
91
92                 it("should contain ${OnapMdc.CLIENT_IP}") {
93                     assertThat(mdc[OnapMdc.CLIENT_IP]).isEqualTo(clientIp)
94                 }
95             }
96         }
97     }
98 })