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