b5f35c3a165f20983cec356d7dcbe357fd3c2de4
[aaf/authz.git] / auth / auth-certman / src / test / java / org / onap / aaf / auth / cm / cert / JU_BCFactory.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.cm.cert;
24
25 import static org.junit.Assert.assertNotNull;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.security.Key;
33 import java.security.PrivateKey;
34 import java.security.PublicKey;
35
36 import org.bouncycastle.operator.OperatorCreationException;
37 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
38 import org.junit.BeforeClass;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mockito;
44 import org.mockito.runners.MockitoJUnitRunner;
45 import org.onap.aaf.cadi.configure.CertException;
46 import org.onap.aaf.misc.env.TimeTaken;
47 import org.onap.aaf.misc.env.Trans;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class JU_BCFactory {
51     
52     private static BCFactory bcFactory = new BCFactory();
53     
54     private static BCFactory bcFact;
55     
56     private static PrivateKey pk;
57     
58     
59     private static Trans trans;
60     
61     
62     private static PKCS10CertificationRequest req;
63     
64     @BeforeClass
65     public static void setUp() throws IOException {
66         pk = new XYZKey();
67         trans = mock(Trans.class);
68         req = mock(PKCS10CertificationRequest.class);
69         when(req.getEncoded()).thenReturn(new byte[1]);
70         when(trans.start(Mockito.anyString(), Mockito.anyInt())).thenReturn(new TimeTaken(null, 0) {
71             
72             @Override
73             public void output(StringBuilder sb) {
74                 // TODO Auto-generated method stub
75                 
76             }
77         });
78         bcFact = mock(BCFactory.class);
79     }
80     
81     @Test
82     public void toStrin() throws OperatorCreationException, IOException, CertException {
83         assertNotNull(bcFactory.toString(req));
84     }
85     
86     @Test
87     public void toStrinMoc() throws OperatorCreationException, IOException, CertException {
88         assertNotNull(bcFact.toString(req));
89     }
90     
91     @Rule
92     public ExpectedException thrown= ExpectedException.none();
93     
94     @Test
95     public void toCSR()  {
96         try {
97             assertNotNull(bcFactory.toCSR(trans, new File("/random/path")));
98             thrown.expect(FileNotFoundException.class);
99         } catch (IOException e) {
100             e.printStackTrace();
101         }
102     }
103     
104 }
105
106 class XYZKey implements Key, PublicKey, PrivateKey {
107     
108     int rotValue;
109     public XYZKey() {
110         rotValue = 1200213;
111     }
112     public String getAlgorithm() {
113         return "XYZ";
114     }
115
116     public String getFormat() {
117         return "XYZ Special Format";
118     }
119
120     public byte[] getEncoded() {
121         byte b[] = new byte[4];
122         b[3] = (byte) ((rotValue << 24) & 0xff);
123         b[2] = (byte) ((rotValue << 16) & 0xff);
124         b[1] = (byte) ((rotValue << 8) & 0xff);
125         b[0] = (byte) ((rotValue << 0) & 0xff);
126         return b;
127     }
128 }