AT&T 2.0.19 Code drop, stage 3
[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 package org.onap.aaf.auth.cm.cert;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27
28 import java.io.File;
29 import java.io.FileNotFoundException;
30 import java.io.IOException;
31 import java.security.Key;
32 import java.security.PrivateKey;
33 import java.security.PublicKey;
34
35 import org.bouncycastle.operator.OperatorCreationException;
36 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
37 import org.junit.BeforeClass;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.rules.ExpectedException;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mockito;
43 import org.mockito.runners.MockitoJUnitRunner;
44 import org.onap.aaf.cadi.cm.CertException;
45 import org.onap.aaf.misc.env.TimeTaken;
46 import org.onap.aaf.misc.env.Trans;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class JU_BCFactory {
50         
51         private static BCFactory bcFactory = new BCFactory();
52         
53         private static BCFactory bcFact;
54         
55         private static PrivateKey pk;
56         
57         
58         private static Trans trans;
59         
60         
61         private static PKCS10CertificationRequest req;
62         
63         @BeforeClass
64         public static void setUp() throws IOException {
65                 pk = new XYZKey();
66                 trans = mock(Trans.class);
67                 req = mock(PKCS10CertificationRequest.class);
68                 when(req.getEncoded()).thenReturn(new byte[1]);
69                 when(trans.start(Mockito.anyString(), Mockito.anyInt())).thenReturn(new TimeTaken(null, 0) {
70                         
71                         @Override
72                         public void output(StringBuilder sb) {
73                                 // TODO Auto-generated method stub
74                                 
75                         }
76                 });
77                 bcFact = mock(BCFactory.class);
78         }
79         
80         @Test
81         public void toStrin() throws OperatorCreationException, IOException, CertException {
82                 assertNotNull(bcFactory.toString(req));
83         }
84         
85         @Test
86         public void toStrinMoc() throws OperatorCreationException, IOException, CertException {
87                 assertNotNull(bcFact.toString(req));
88         }
89         
90         @Rule
91     public ExpectedException thrown= ExpectedException.none();
92         
93         @Test
94         public void toCSR()  {
95                 try {
96                         assertNotNull(bcFactory.toCSR(trans, new File("/random/path")));
97                         thrown.expect(FileNotFoundException.class);
98                 } catch (IOException e) {
99                         
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 }