Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-els-onap-logging / src / test / java / org / onap / aai / logging / CNNameTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
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
21 package org.onap.aai.logging;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
25
26 import ch.qos.logback.access.spi.IAccessEvent;
27
28 import java.security.cert.X509Certificate;
29
30 import javax.security.auth.x500.X500Principal;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.mockito.Spy;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.springframework.mock.web.MockHttpServletRequest;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class CNNameTest {
43     @Mock
44     X509Certificate cert;
45
46     @Mock
47     IAccessEvent accessEvent;
48
49     @Spy
50     @InjectMocks
51     private CNName cnName;
52
53     @Before
54     public void setup() {
55         when(cnName.isStarted()).thenReturn(true);
56     }
57
58     @Test
59     public void basicAuthTest() {
60
61         MockHttpServletRequest https = new MockHttpServletRequest();
62         https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA==");
63         https.setAttribute("javax.servlet.request.cipher_suite", "");
64         https.setAttribute("javax.servlet.request.X509Certificate", null);
65
66         when(accessEvent.getRequest()).thenReturn(https);
67         assertEquals("user", cnName.convert(accessEvent));
68
69     }
70
71     @Test
72     public void incorrectHeaderBasicAuthTest() {
73
74         MockHttpServletRequest https = new MockHttpServletRequest();
75
76         https.addHeader("Authorization", "dXNlcjpwYXNzd29yZA==");
77         https.setAttribute("javax.servlet.request.cipher_suite", "");
78         https.setAttribute("javax.servlet.request.X509Certificate", null);
79
80         when(accessEvent.getRequest()).thenReturn(https);
81         assertEquals("-", cnName.convert(accessEvent));
82
83     }
84
85     @Test
86     public void noCipherSuiteTest() {
87
88         MockHttpServletRequest https = new MockHttpServletRequest();
89
90         https.addHeader("Authorization", "Basic dXNlcjpwYXNzd29yZA==");
91         https.setAttribute("javax.servlet.request.cipher_suite", null);
92         https.setAttribute("javax.servlet.request.X509Certificate", null);
93
94         when(accessEvent.getRequest()).thenReturn(https);
95         assertEquals("-", cnName.convert(accessEvent));
96
97     }
98
99     @Test
100     public void certificateTest() {
101         String testSubject = "CN=TestName, OU=TestOU, O=TestOrg, C=Country";
102         X509Certificate[] certChain = {cert};
103         MockHttpServletRequest https = new MockHttpServletRequest();
104
105         https.setAttribute("javax.servlet.request.cipher_suite", "");
106         https.setAttribute("javax.servlet.request.X509Certificate", certChain);
107
108         when(accessEvent.getRequest()).thenReturn(https);
109         when(cert.getSubjectX500Principal()).thenReturn(new X500Principal(testSubject));
110
111         assertEquals(testSubject, cnName.convert(accessEvent));
112     }
113
114 }