1414
1515package org .hyperledger .fabric_ca .sdk ;
1616
17+ import java .io .File ;
1718import java .net .MalformedURLException ;
18-
19+ import java .util .Properties ;
20+
21+ import org .hyperledger .fabric .sdk .exception .CryptoException ;
22+ import org .hyperledger .fabric .sdk .security .CryptoPrimitives ;
23+ import org .hyperledger .fabric .sdk .security .CryptoSuite ;
24+ import org .hyperledger .fabric .sdkintegration .SampleStore ;
25+ import org .hyperledger .fabric .sdkintegration .SampleUser ;
26+ import org .hyperledger .fabric_ca .sdk .exception .EnrollmentException ;
1927import org .hyperledger .fabric_ca .sdk .exception .InvalidArgumentException ;
28+ import org .hyperledger .fabric_ca .sdk .exception .RegistrationException ;
2029import org .junit .Assert ;
30+ import org .junit .Before ;
2131import org .junit .Test ;
2232
2333public class HFCAClientTest {
2434 public static class MemberServicesFabricCAImplTest {
35+ private static final String TEST_ADMIN_NAME = "admin" ;
36+ private static final String TEST_ADMIN_PW = "adminpw" ;
37+ private static final String TEST_ADMIN_ORG = "org1" ;
38+
39+ private SampleStore sampleStore ;
40+ SampleUser admin ;
41+
42+ @ Before
43+ public void setup () throws CryptoException , InvalidArgumentException , org .hyperledger .fabric .sdk .exception .InvalidArgumentException , MalformedURLException , EnrollmentException {
44+
45+ File sampleStoreFile = new File (System .getProperty ("java.io.tmpdir" ) + "/HFCSampletest.properties" );
46+ if (sampleStoreFile .exists ()) { //For testing start fresh
47+ sampleStoreFile .delete ();
48+ }
49+ sampleStore = new SampleStore (sampleStoreFile );
50+ sampleStoreFile .deleteOnExit ();
51+
52+ //SampleUser can be any implementation that implements org.hyperledger.fabric.sdk.User Interface
53+ admin = sampleStore .getMember (TEST_ADMIN_NAME , TEST_ADMIN_ORG );
54+
55+ }
2556
2657 @ Test
2758 public void testCOPCreation () {
@@ -44,7 +75,7 @@ public void testNullURL() {
4475 Assert .fail ("Expected exception" );
4576
4677 } catch (Exception e ) {
47- Assert .assertSame (e .getClass (), MalformedURLException . class );
78+ Assert .assertSame (MalformedURLException . class , e .getClass ());
4879
4980 }
5081 }
@@ -57,7 +88,7 @@ public void emptyURL() {
5788 Assert .fail ("Expected exception" );
5889
5990 } catch (Exception e ) {
60- Assert .assertSame (e .getClass (), MalformedURLException . class );
91+ Assert .assertSame (MalformedURLException . class , e .getClass ());
6192
6293 }
6394 }
@@ -70,7 +101,7 @@ public void testBadProto() {
70101 Assert .fail ("Expected exception" );
71102
72103 } catch (Exception e ) {
73- Assert .assertSame (e .getClass (), IllegalArgumentException . class );
104+ Assert .assertSame (IllegalArgumentException . class , e .getClass ());
74105
75106 }
76107 }
@@ -83,7 +114,7 @@ public void testBadURLPath() {
83114 Assert .fail ("Expected exception" );
84115
85116 } catch (Exception e ) {
86- Assert .assertSame (e .getClass (), IllegalArgumentException . class );
117+ Assert .assertSame (IllegalArgumentException . class , e .getClass ());
87118
88119 }
89120 }
@@ -96,7 +127,7 @@ public void testBadURLQuery() {
96127 Assert .fail ("Expected exception" );
97128
98129 } catch (Exception e ) {
99- Assert .assertSame (e .getClass (), IllegalArgumentException . class );
130+ Assert .assertSame (IllegalArgumentException . class , e .getClass ());
100131
101132 }
102133 }
@@ -110,7 +141,7 @@ public void testNewInstanceNameUrlProperties() {
110141 Assert .assertSame (HFCAClient .class , memberServices .getClass ());
111142
112143 } catch (Exception e ) {
113- Assert .assertSame (e .getClass (), IllegalArgumentException . class );
144+ Assert .assertSame (IllegalArgumentException . class , e .getClass ());
114145
115146 }
116147 }
@@ -123,8 +154,8 @@ public void testNewInstanceNameUrlPropertiesSetNullName() {
123154 Assert .fail ("Expected exception when name is set to null" );
124155
125156 } catch (Exception e ) {
126- Assert .assertSame (e .getClass (), InvalidArgumentException . class );
127- Assert .assertEquals (e . getMessage (), "name must not be null or an empty string." );
157+ Assert .assertSame (InvalidArgumentException . class , e .getClass ());
158+ Assert .assertEquals ("name must not be null or an empty string." , e . getMessage () );
128159
129160 }
130161 }
@@ -137,8 +168,228 @@ public void testNewInstanceNameUrlPropertiesSetEmptyName() {
137168 Assert .fail ("Expected exception when name is set to null" );
138169
139170 } catch (Exception e ) {
140- Assert .assertSame (e .getClass (), InvalidArgumentException .class );
141- Assert .assertEquals (e .getMessage (), "name must not be null or an empty string." );
171+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
172+ Assert .assertEquals ("name must not be null or an empty string." , e .getMessage ());
173+
174+ }
175+ }
176+
177+ @ Test
178+ public void testNewInstanceNoHost () {
179+ Properties testprops = new Properties ();
180+
181+ try {
182+ HFCAClient .createNewInstance ("client" , "http://:99" , testprops );
183+ Assert .fail ("Expected exception when hostname is not specified in the URL" );
184+
185+ } catch (Exception e ) {
186+ Assert .assertSame (IllegalArgumentException .class , e .getClass ());
187+ Assert .assertEquals ("HFCAClient url needs host" , e .getMessage ());
188+
189+ }
190+ }
191+
192+ @ Test
193+ public void testGetCryptoSuite () {
194+ CryptoPrimitives testcrypt = new CryptoPrimitives ();
195+
196+ try {
197+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
198+ client .setCryptoSuite (testcrypt );
199+ Assert .assertEquals (testcrypt , client .getCryptoSuite ());
200+
201+ } catch (Exception e ) {
202+ Assert .fail ("Unexpected Exception " + e .getMessage ());
203+ }
204+ }
205+
206+ @ Test
207+ public void testRegisterNullEnrollId () {
208+
209+ try {
210+ RegistrationRequest regreq = new RegistrationRequest ("name" , "affiliation" );
211+ regreq .setEnrollmentID (null );
212+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
213+ client .register (regreq , null );
214+ Assert .fail ("Expected exception when enrollment ID is null" );
215+
216+ } catch (Exception e ) {
217+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
218+ Assert .assertEquals ("EntrollmentID cannot be null or empty" , e .getMessage ());
219+
220+ }
221+ }
222+
223+ @ Test
224+ public void testRegisterEmptyEnrollId () {
225+
226+ try {
227+ RegistrationRequest regreq = new RegistrationRequest ("name" , "affiliation" );
228+ regreq .setEnrollmentID ("" );
229+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
230+ client .register (regreq , null );
231+ Assert .fail ("Expected exception when enrollment ID is empty" );
232+
233+ } catch (Exception e ) {
234+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
235+ Assert .assertEquals ("EntrollmentID cannot be null or empty" , e .getMessage ());
236+
237+ }
238+ }
239+
240+ @ Test
241+ public void testRegisterNoServerResponse () {
242+
243+ try {
244+ RegistrationRequest regreq = new RegistrationRequest ("name" , "affiliation" );
245+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
246+ client .register (regreq , admin );
247+ Assert .fail ("Expected exception when server is not available during registration" );
248+
249+ } catch (Exception e ) {
250+ Assert .assertSame (RegistrationException .class , e .getClass ());
251+ Assert .assertTrue (e .getMessage ().contains ("Error while registering the user" ));
252+
253+ }
254+ }
255+
256+ @ Test
257+ public void testEnrollmentEmptyUser () {
258+
259+ try {
260+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
261+ client .enroll ("" , TEST_ADMIN_PW );
262+ Assert .fail ("Expected exception when user parameter is empty" );
263+
264+ } catch (Exception e ) {
265+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
266+ Assert .assertTrue (e .getMessage ().contains ("enrollment user is not set" ));
267+
268+ }
269+ }
270+
271+ @ Test
272+ public void testEnrollmentNullUser () {
273+
274+ try {
275+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
276+ client .enroll (null , TEST_ADMIN_PW );
277+ Assert .fail ("Expected exception when user parameter is null" );
278+
279+ } catch (Exception e ) {
280+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
281+ Assert .assertTrue (e .getMessage ().contains ("enrollment user is not set" ));
282+
283+ }
284+ }
285+
286+ @ Test
287+ public void testEnrollmentEmptySecret () {
288+
289+ try {
290+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
291+ client .enroll (TEST_ADMIN_NAME , "" );
292+ Assert .fail ("Expected exception when secret parameter is empty" );
293+
294+ } catch (Exception e ) {
295+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
296+ Assert .assertTrue (e .getMessage ().contains ("enrollment secret is not set" ));
297+
298+ }
299+ }
300+
301+ @ Test
302+ public void testEnrollmentNullSecret () {
303+
304+ try {
305+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
306+ client .enroll (TEST_ADMIN_NAME , null );
307+ Assert .fail ("Expected exception when secret parameter is null" );
308+
309+ } catch (Exception e ) {
310+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
311+ Assert .assertTrue (e .getMessage ().contains ("enrollment secret is not set" ));
312+
313+ }
314+ }
315+
316+ @ Test
317+ public void testEnrollmentNoServerResponse () {
318+
319+ try {
320+ CryptoSuite cryptoSuite = CryptoSuite .Factory .getCryptoSuite ();
321+ cryptoSuite .init ();
322+ EnrollmentRequest req = new EnrollmentRequest ("profile 1" , "label 1" , null );
323+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
324+ client .setCryptoSuite (cryptoSuite );
325+ client .enroll (TEST_ADMIN_NAME , TEST_ADMIN_NAME , req );
326+ Assert .fail ("Expected exception when server is not available during enrollment" );
327+
328+ } catch (Exception e ) {
329+ Assert .assertSame (EnrollmentException .class , e .getClass ());
330+ Assert .assertTrue (e .getMessage ().contains ("Url:http://localhost:99, Failed to enroll user admin " ));
331+
332+ }
333+ }
334+
335+ @ Test
336+ public void testReenrollmentNullUser () {
337+
338+ try {
339+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
340+ client .reenroll (null );
341+ Assert .fail ("Expected exception when user parameter is null" );
342+
343+ } catch (Exception e ) {
344+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
345+ Assert .assertTrue (e .getMessage ().contains ("reenrollment user is missing" ));
346+
347+ }
348+ }
349+
350+ @ Test
351+ public void testReenrollmentNullEnrollmentObject () {
352+
353+ try {
354+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
355+ admin .setEnrollment (null );
356+ client .reenroll (admin );
357+ Assert .fail ("Expected exception when user enrollment object is null" );
358+
359+ } catch (Exception e ) {
360+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
361+ Assert .assertTrue (e .getMessage ().contains ("reenrollment user is not a valid user object" ));
362+
363+ }
364+ }
365+
366+ @ Test
367+ public void testRevokeNullUserObject () {
368+
369+ try {
370+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
371+ client .revoke (null , admin .getName (), "keyCompromise" );
372+ Assert .fail ("Expected exception when revoker object is null" );
373+
374+ } catch (Exception e ) {
375+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
376+ Assert .assertTrue (e .getMessage ().contains ("revoker is not set" ));
377+
378+ }
379+ }
380+
381+ @ Test
382+ public void testRevokeNullEnrollmentObject () {
383+
384+ try {
385+ HFCAClient client = HFCAClient .createNewInstance ("client" , "http://localhost:99" , null );
386+ admin .setEnrollment (null );
387+ client .revoke (admin , admin .getEnrollment (), "keyCompromise" );
388+ Assert .fail ("Expected exception when enrollment object is null" );
389+
390+ } catch (Exception e ) {
391+ Assert .assertSame (InvalidArgumentException .class , e .getClass ());
392+ Assert .assertTrue (e .getMessage ().contains ("revokee enrollment is not set" ));
142393
143394 }
144395 }
0 commit comments