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