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.util;
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;
33 import javax.security.auth.x500.X500Principal;
34 import javax.servlet.http.HttpServletRequest;
35 import java.security.cert.X509Certificate;
37 import static org.junit.Assert.assertTrue;
38 import static org.mockito.Mockito.when;
40 @PowerMockIgnore("javax.security.auth.x500.X500Principal")
41 @PrepareForTest({IAccessEvent.class, HttpServletRequest.class, X509Certificate.class})
42 public class CNNameTest {
45 PowerMockAgent.initializeIfNeeded();
49 IAccessEvent mockAccEvent;
50 HttpServletRequest mockHttpServletRequest;
58 public void initialize(){
59 mockAccEvent = Mockito.mock(IAccessEvent.class);
60 mockHttpServletRequest = Mockito.mock(HttpServletRequest.class);
61 cert = Mockito.mock(X509Certificate.class);
66 * Test 'convert' when there is no AccessConverter.
69 public void testConvert_withoutAccessConverter(){
70 cnname = getTestObj(false);
71 assertTrue("Conversion failed with no AccessConverter", "INACTIVE_HEADER_CONV".equals(cnname.convert(mockAccEvent)));
75 * Test 'convert' with no CipherSuite.
78 public void testConvert_withNullCipherSuite(){
79 setupForCipherSuite(null);
80 assertTrue("Conversion failed for a null CipherSuite", "-".equals(cnname.convert(mockAccEvent)));
85 * Test 'convert' with a non-null CipherSuite.
88 public void testConvert_withNotNullCipherSuite(){
90 setupForCipherSuite("StrRepOfAValidSuite");
92 final X500Principal principal = new X500Principal("CN=AAI, OU=DOX, O=BWS, C=CA");
94 Mockito.when(cert.getSubjectX500Principal()).thenReturn(principal);
96 final X509Certificate[] certChain = {cert};
98 when(mockHttpServletRequest.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(certChain);
100 assertTrue("Conversion failed for a valid CipherSuite", principal.toString().equals(cnname.convert(mockAccEvent)));
105 * Helper method to mock IAccessEvent and HttpServletRequest.
107 * @param suite CipherSuite to be used in current test
109 private void setupForCipherSuite(String suite){
110 cnname = getTestObj(true);
111 when(mockAccEvent.getRequest()).thenReturn(mockHttpServletRequest);
112 when(mockHttpServletRequest.getAttribute("javax.servlet.request.cipher_suite")).thenReturn(suite);
117 * Helper method to create a CNName object with overridden 'start status' .
119 * @param instanceStarted Start status to be used
120 * @return CNName object to test
122 private CNName getTestObj(final boolean instanceStarted){
125 public boolean isStarted(){
126 return instanceStarted;