d5cd62cd4867c124ca48d3d484438e5ad2142d31
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / logging / CNNameTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.logging;
23
24 import ch.qos.logback.access.spi.IAccessEvent;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.onap.aai.logging.CNName;
29 import org.onap.aai.serialization.queryformats.QueryFormatTestHelper;
30 import org.onap.aai.util.AAIConstants;
31 import org.powermock.core.classloader.annotations.PowerMockIgnore;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.agent.PowerMockAgent;
34
35 import javax.security.auth.x500.X500Principal;
36 import javax.servlet.http.HttpServletRequest;
37 import java.security.cert.X509Certificate;
38
39 import static org.junit.Assert.assertTrue;
40 import static org.mockito.Mockito.when;
41
42 @PowerMockIgnore("javax.security.auth.x500.X500Principal")
43 @PrepareForTest({IAccessEvent.class, HttpServletRequest.class, X509Certificate.class})
44 public class CNNameTest {
45
46         static {
47              PowerMockAgent.initializeIfNeeded();
48          }
49         
50         
51         IAccessEvent mockAccEvent;
52         HttpServletRequest mockHttpServletRequest;
53         CNName cnname;
54         X509Certificate cert;
55
56         /**
57          * Initialize.
58          */
59         @Before
60         public void initialize() throws Exception {
61                 System.setProperty("AJSC_HOME", ".");
62                 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
63                 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/bundleconfig-local/etc/oxm/");
64                 mockAccEvent = Mockito.mock(IAccessEvent.class);
65                 mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
66                 cert = Mockito.mock(X509Certificate.class);
67         }
68
69         
70         /**
71          * Test 'convert' when there is no AccessConverter.
72          */
73         @Test
74         public void testConvert_withoutAccessConverter(){
75                 cnname = getTestObj(false);
76                 assertTrue("Conversion failed with no AccessConverter", "INACTIVE_HEADER_CONV".equals(cnname.convert(mockAccEvent)));
77         }
78
79         /**
80          * Test 'convert' with no CipherSuite.
81          */
82         @Test
83         public void testConvert_withNullCipherSuite(){
84                 setupForCipherSuite(null);
85                 assertTrue("Conversion failed for a null CipherSuite", "-".equals(cnname.convert(mockAccEvent)));
86         }
87         
88         
89         /**
90          * Test 'convert' with a non-null CipherSuite.
91          */
92         @Test
93         public void testConvert_withNotNullCipherSuite(){
94                 
95                 setupForCipherSuite("StrRepOfAValidSuite");
96
97                 final X500Principal principal = new X500Principal("CN=AAI, OU=DOX, O=BWS, C=CA");
98                 
99                 Mockito.when(cert.getSubjectX500Principal()).thenReturn(principal);
100                 
101                 final X509Certificate[] certChain = {cert};
102                 
103                 when(mockHttpServletRequest.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(certChain);
104
105                 assertTrue("Conversion failed for a valid CipherSuite", principal.toString().equals(cnname.convert(mockAccEvent)));
106         }
107         
108         
109         /**
110          * Helper method to mock IAccessEvent and HttpServletRequest.
111          *
112          * @param suite CipherSuite to be used in current test
113          */
114         private void setupForCipherSuite(String suite){
115                 cnname = getTestObj(true);
116                 when(mockAccEvent.getRequest()).thenReturn(mockHttpServletRequest);
117                 when(mockHttpServletRequest.getAttribute("javax.servlet.request.cipher_suite")).thenReturn(suite);
118         }
119         
120                 
121         /**
122          * Helper method to create a CNName object with overridden 'start status' .
123          *
124          * @param instanceStarted Start status to be used
125          * @return CNName object to test
126          */
127         private CNName getTestObj(final boolean instanceStarted){
128                 return new CNName(){
129                         @Override
130                         public boolean isStarted(){
131                                 return instanceStarted;
132                         }
133                 };
134         }
135 }
136
137
138