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