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