[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-certman / src / test / java / com / att / authz / cm / cert / JU_BCFactory.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.authz.cm.cert;\r
24 \r
25 import static org.junit.Assert.*;\r
26 import static org.mockito.Mockito.mock;\r
27 import static org.mockito.Mockito.when;\r
28 \r
29 import java.io.File;\r
30 import java.io.FileNotFoundException;\r
31 import java.io.IOException;\r
32 import java.security.Key;\r
33 import java.security.PrivateKey;\r
34 import java.security.PublicKey;\r
35 \r
36 import org.bouncycastle.operator.OperatorCreationException;\r
37 import org.bouncycastle.pkcs.PKCS10CertificationRequest;\r
38 import org.junit.BeforeClass;\r
39 import org.junit.Rule;\r
40 import org.junit.Test;\r
41 import org.junit.rules.ExpectedException;\r
42 import org.junit.runner.RunWith;\r
43 import org.mockito.Mock;\r
44 import org.mockito.Mockito;\r
45 import org.mockito.runners.MockitoJUnitRunner;\r
46 \r
47 import com.att.cadi.cm.CertException;\r
48 import com.att.inno.env.TimeTaken;\r
49 import com.att.inno.env.Trans;\r
50 \r
51 @RunWith(MockitoJUnitRunner.class)\r
52 public class JU_BCFactory {\r
53         \r
54         private static BCFactory bcFactory = new BCFactory();\r
55         \r
56         private static BCFactory bcFact;\r
57         \r
58         private static PrivateKey pk;\r
59         \r
60         \r
61         private static Trans trans;\r
62         \r
63         \r
64         private static PKCS10CertificationRequest req;\r
65         \r
66         @BeforeClass\r
67         public static void setUp() throws IOException {\r
68                 pk = new XYZKey();\r
69                 trans = mock(Trans.class);\r
70                 req = mock(PKCS10CertificationRequest.class);\r
71                 when(req.getEncoded()).thenReturn(new byte[1]);\r
72                 when(trans.start(Mockito.anyString(), Mockito.anyInt())).thenReturn(new TimeTaken(null, 0) {\r
73                         \r
74                         @Override\r
75                         public void output(StringBuilder sb) {\r
76                                 // TODO Auto-generated method stub\r
77                                 \r
78                         }\r
79                 });\r
80                 bcFact = mock(BCFactory.class);\r
81         }\r
82         \r
83         @Test\r
84         public void toStrin() throws OperatorCreationException, IOException, CertException {\r
85                 assertNotNull(bcFactory.toString(trans, req));\r
86         }\r
87         \r
88         @Test\r
89         public void toStrinMoc() throws OperatorCreationException, IOException, CertException {\r
90                 assertNotNull(bcFact.toString(trans, req));\r
91         }\r
92         \r
93         @Rule\r
94     public ExpectedException thrown= ExpectedException.none();\r
95         \r
96         @Test\r
97         public void toCSR()  {\r
98                 try {\r
99                         assertNotNull(bcFactory.toCSR(trans, new File("/random/path")));\r
100                         thrown.expect(FileNotFoundException.class);\r
101                 } catch (IOException e) {\r
102                         \r
103                         e.printStackTrace();\r
104                 }\r
105         }\r
106         \r
107 }\r
108 \r
109 class XYZKey implements Key, PublicKey, PrivateKey {\r
110         \r
111         int rotValue;\r
112         public XYZKey() {\r
113                 rotValue = 1200213;\r
114         }\r
115         public String getAlgorithm() {\r
116                 return "XYZ";\r
117         }\r
118 \r
119         public String getFormat() {\r
120                 return "XYZ Special Format";\r
121         }\r
122 \r
123         public byte[] getEncoded() {\r
124                 byte b[] = new byte[4];\r
125                 b[3] = (byte) ((rotValue << 24) & 0xff);\r
126                 b[2] = (byte) ((rotValue << 16) & 0xff);\r
127                 b[1] = (byte) ((rotValue << 8) & 0xff);\r
128                 b[0] = (byte) ((rotValue << 0) & 0xff);\r
129                 return b;\r
130         }\r
131 }\r