2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.logging;
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;
34 import javax.security.auth.x500.X500Principal;
35 import javax.servlet.http.HttpServletRequest;
36 import java.security.cert.X509Certificate;
38 import static org.junit.Assert.assertTrue;
39 import static org.mockito.Mockito.when;
41 @PowerMockIgnore("javax.security.auth.x500.X500Principal")
42 @PrepareForTest({IAccessEvent.class, HttpServletRequest.class, X509Certificate.class})
43 public class CNNameTest {
46 PowerMockAgent.initializeIfNeeded();
50 IAccessEvent mockAccEvent;
51 HttpServletRequest mockHttpServletRequest;
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);
70 * Test 'convert' when there is no AccessConverter.
73 public void testConvert_withoutAccessConverter(){
74 cnname = getTestObj(false);
75 assertTrue("Conversion failed with no AccessConverter", "INACTIVE_HEADER_CONV".equals(cnname.convert(mockAccEvent)));
79 * Test 'convert' with no CipherSuite.
82 public void testConvert_withNullCipherSuite(){
83 setupForCipherSuite(null);
84 assertTrue("Conversion failed for a null CipherSuite", "-".equals(cnname.convert(mockAccEvent)));
89 * Test 'convert' with a non-null CipherSuite.
92 public void testConvert_withNotNullCipherSuite(){
94 setupForCipherSuite("StrRepOfAValidSuite");
96 final X500Principal principal = new X500Principal("CN=AAI, OU=DOX, O=BWS, C=CA");
98 Mockito.when(cert.getSubjectX500Principal()).thenReturn(principal);
100 final X509Certificate[] certChain = {cert};
102 when(mockHttpServletRequest.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(certChain);
104 assertTrue("Conversion failed for a valid CipherSuite", principal.toString().equals(cnname.convert(mockAccEvent)));
109 * Helper method to mock IAccessEvent and HttpServletRequest.
111 * @param suite CipherSuite to be used in current test
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);
121 * Helper method to create a CNName object with overridden 'start status' .
123 * @param instanceStarted Start status to be used
124 * @return CNName object to test
126 private CNName getTestObj(final boolean instanceStarted){
129 public boolean isStarted(){
130 return instanceStarted;