6b2536c9db2aae4d129d13e99ac57e23937b1e06
[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.openecomp.aai.logging.CNName;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.mockito.Mockito;
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(){
59                 System.setProperty("AJSC_HOME", ".");
60                 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
61                 mockAccEvent = Mockito.mock(IAccessEvent.class);
62                 mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
63                 cert = Mockito.mock(X509Certificate.class);
64         }
65
66         
67         /**
68          * Test 'convert' when there is no AccessConverter.
69          */
70         @Test
71         public void testConvert_withoutAccessConverter(){
72                 cnname = getTestObj(false);
73                 assertTrue("Conversion failed with no AccessConverter", "INACTIVE_HEADER_CONV".equals(cnname.convert(mockAccEvent)));
74         }
75
76         /**
77          * Test 'convert' with no CipherSuite.
78          */
79         @Test
80         public void testConvert_withNullCipherSuite(){
81                 setupForCipherSuite(null);
82                 assertTrue("Conversion failed for a null CipherSuite", "-".equals(cnname.convert(mockAccEvent)));
83         }
84         
85         
86         /**
87          * Test 'convert' with a non-null CipherSuite.
88          */
89         @Test
90         public void testConvert_withNotNullCipherSuite(){
91                 
92                 setupForCipherSuite("StrRepOfAValidSuite");
93
94                 final X500Principal principal = new X500Principal("CN=AAI, OU=DOX, O=BWS, C=CA");
95                 
96                 Mockito.when(cert.getSubjectX500Principal()).thenReturn(principal);
97                 
98                 final X509Certificate[] certChain = {cert};
99                 
100                 when(mockHttpServletRequest.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(certChain);
101
102                 assertTrue("Conversion failed for a valid CipherSuite", principal.toString().equals(cnname.convert(mockAccEvent)));
103         }
104         
105         
106         /**
107          * Helper method to mock IAccessEvent and HttpServletRequest.
108          *
109          * @param suite CipherSuite to be used in current test
110          */
111         private void setupForCipherSuite(String suite){
112                 cnname = getTestObj(true);
113                 when(mockAccEvent.getRequest()).thenReturn(mockHttpServletRequest);
114                 when(mockHttpServletRequest.getAttribute("javax.servlet.request.cipher_suite")).thenReturn(suite);
115         }
116         
117                 
118         /**
119          * Helper method to create a CNName object with overridden 'start status' .
120          *
121          * @param instanceStarted Start status to be used
122          * @return CNName object to test
123          */
124         private CNName getTestObj(final boolean instanceStarted){
125                 return new CNName(){
126                         @Override
127                         public boolean isStarted(){
128                                 return instanceStarted;
129                         }
130                 };
131         }
132 }
133
134
135