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