2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.logging;
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;
35 import javax.security.auth.x500.X500Principal;
36 import javax.servlet.http.HttpServletRequest;
37 import java.security.cert.X509Certificate;
39 import static org.junit.Assert.assertTrue;
40 import static org.mockito.Mockito.when;
42 @PowerMockIgnore("javax.security.auth.x500.X500Principal")
43 @PrepareForTest({IAccessEvent.class, HttpServletRequest.class, X509Certificate.class})
44 public class CNNameTest {
47 PowerMockAgent.initializeIfNeeded();
51 IAccessEvent mockAccEvent;
52 HttpServletRequest mockHttpServletRequest;
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);
71 * Test 'convert' when there is no AccessConverter.
74 public void testConvert_withoutAccessConverter(){
75 cnname = getTestObj(false);
76 assertTrue("Conversion failed with no AccessConverter", "INACTIVE_HEADER_CONV".equals(cnname.convert(mockAccEvent)));
80 * Test 'convert' with no CipherSuite.
83 public void testConvert_withNullCipherSuite(){
84 setupForCipherSuite(null);
85 assertTrue("Conversion failed for a null CipherSuite", "-".equals(cnname.convert(mockAccEvent)));
90 * Test 'convert' with a non-null CipherSuite.
93 public void testConvert_withNotNullCipherSuite(){
95 setupForCipherSuite("StrRepOfAValidSuite");
97 final X500Principal principal = new X500Principal("CN=AAI, OU=DOX, O=BWS, C=CA");
99 Mockito.when(cert.getSubjectX500Principal()).thenReturn(principal);
101 final X509Certificate[] certChain = {cert};
103 when(mockHttpServletRequest.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(certChain);
105 assertTrue("Conversion failed for a valid CipherSuite", principal.toString().equals(cnname.convert(mockAccEvent)));
110 * Helper method to mock IAccessEvent and HttpServletRequest.
112 * @param suite CipherSuite to be used in current test
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);
122 * Helper method to create a CNName object with overridden 'start status' .
124 * @param instanceStarted Start status to be used
125 * @return CNName object to test
127 private CNName getTestObj(final boolean instanceStarted){
130 public boolean isStarted(){
131 return instanceStarted;