Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-cass / src / test / java / org / onap / aaf / auth / direct / test / JU_DirectCertIdentity.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
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
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  ******************************************************************************/
22
23 package org.onap.aaf.auth.direct.test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.security.Principal;
29 import java.security.cert.CertificateException;
30 import java.security.cert.X509Certificate;
31 import java.util.ArrayList;
32 import java.util.List;
33
34 import javax.servlet.http.HttpServletRequest;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.onap.aaf.auth.dao.cached.CachedCertDAO;
42 import org.onap.aaf.auth.dao.cass.CertDAO;
43 import org.onap.aaf.auth.direct.DirectCertIdentity;
44 import org.onap.aaf.auth.env.AuthzTrans;
45 import org.onap.aaf.auth.layer.Result;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 @RunWith(PowerMockRunner.class)
49 public class JU_DirectCertIdentity {
50
51     public DirectCertIdentity directCertIdentity;
52
53     @Before
54     public void setUp() {
55         directCertIdentity = new DirectCertIdentity();
56     }
57
58     @Mock
59     HttpServletRequest req;
60     X509Certificate cert;
61     byte[] _certBytes;
62
63     @Test
64     public void testidentity() {
65
66         try {
67             Principal p = directCertIdentity.identity(req, cert, _certBytes);
68             assertEquals(((p) == null), true);
69             
70             cert = Mockito.mock(X509Certificate.class);
71             Mockito.when(cert.getEncoded()).thenReturn(new byte[128]);
72             
73             Result<List<CertDAO.Data>> rs = new Result<List<CertDAO.Data>>(null, 1, "test", new Object[0]);
74             
75             CachedCertDAO cacheDao = Mockito.mock(CachedCertDAO.class);
76             Mockito.when(cacheDao.read(Mockito.any(AuthzTrans.class),Mockito.any(Object[].class))).thenReturn(rs);
77             DirectCertIdentity.set(cacheDao);
78             p = directCertIdentity.identity(req, cert, _certBytes);
79             
80             _certBytes = new byte[128];
81             List<CertDAO.Data> dataAL = new ArrayList<>();
82             CertDAO.Data data = new CertDAO.Data();
83             dataAL.add(data);
84             rs = new Result<List<CertDAO.Data>>(dataAL, 0, "test", new Object[0]);
85             Mockito.when(cacheDao.read(Mockito.any(AuthzTrans.class),Mockito.any(Object[].class))).thenReturn(rs);
86             DirectCertIdentity.set(cacheDao);
87             p = directCertIdentity.identity(req, cert, _certBytes);
88             assertTrue(p.toString().contains("X509 Authentication for null"));
89             
90             cert = null;
91             directCertIdentity.identity(req, cert, _certBytes);
92         } catch (CertificateException e) {
93             // TODO Auto-generated catch block
94             e.printStackTrace();
95         }
96         // assertTrue(true);
97
98     }
99
100 }