22
33import io .ipfs .api .cbor .*;
44import io .ipfs .cid .*;
5- import io .ipfs .multibase .Multibase ;
65import io .ipfs .multihash .Multihash ;
76import io .ipfs .multiaddr .MultiAddress ;
87import org .junit .*;
98
109import java .io .*;
11- import java .net .URLEncoder ;
1210import java .nio .file .*;
1311import java .util .*;
1412import java .util .function .*;
1513import java .util .stream .*;
1614
15+ import static org .junit .Assert .assertNotNull ;
1716import static org .junit .Assert .assertTrue ;
17+ import static org .junit .Assert .assertEquals ;
18+ import static org .junit .Assert .assertArrayEquals ;
1819
20+ @ SuppressWarnings ({"rawtypes" , "unused" })
1921public class APITest {
2022
2123 private final IPFS ipfs = new IPFS (new MultiAddress ("/ip4/127.0.0.1/tcp/5001" ));
@@ -30,14 +32,14 @@ public void dag() throws IOException {
3032 Cid expected = Cid .decode ("bafyreidbm2zncsc3j25zn7lofgd4woeh6eygdy73thfosuni2rwr3bhcvu" );
3133
3234 Multihash result = put .hash ;
33- Assert . assertTrue ("Correct cid returned" , result . equals ( expected ) );
35+ assertEquals ("Correct cid returned" , result , expected );
3436
3537 byte [] get = ipfs .dag .get (expected );
36- Assert . assertTrue ("Raw data equal" , original . equals ( new String (get ).trim () ));
38+ assertEquals ("Raw data equal" , original , new String (get ).trim ());
3739 Map res = ipfs .dag .resolve ("bafyreidbm2zncsc3j25zn7lofgd4woeh6eygdy73thfosuni2rwr3bhcvu" );
38- Assert . assertTrue ("not resolved" , res != null );
40+ assertNotNull ("not resolved" , res );
3941 res = ipfs .dag .stat (expected );
40- Assert . assertTrue ("not found" , res != null );
42+ assertNotNull ("not found" , res );
4143 }
4244
4345 @ Test
@@ -52,10 +54,11 @@ public void dagCbor() throws IOException {
5254 Cid cid = (Cid ) put .hash ;
5355
5456 byte [] get = ipfs .dag .get (cid );
55- Assert .assertTrue ("Raw data equal" , ((Map )JSONParser .parse (new String (get ))).get ("data" ).equals (value ));
57+ assertEquals ("Raw data equal" , ((Map ) JSONParser .parse (new String (get ))).get ("data" ),
58+ value );
5659
5760 Cid expected = Cid .decode ("zdpuApemz4XMURSCkBr9W5y974MXkSbeDfLeZmiQTPpvkatFF" );
58- Assert . assertTrue ("Correct cid returned" , cid . equals ( expected ) );
61+ assertEquals ("Correct cid returned" , cid , expected );
5962 }
6063
6164 @ Test
@@ -67,14 +70,14 @@ public void keys() throws IOException {
6770 Object rename = ipfs .key .rename (name , newName );
6871 List <KeyInfo > rm = ipfs .key .rm (newName );
6972 List <KeyInfo > remaining = ipfs .key .list ();
70- Assert . assertTrue ("removed key" , remaining . equals ( existing ) );
73+ assertEquals ("removed key" , remaining , existing );
7174 }
7275
7376 @ Test
7477 @ Ignore ("Not reliable" )
7578 public void log () throws IOException {
7679 Map lsResult = ipfs .log .ls ();
77- Assert .assertTrue ("Log ls" , ! lsResult .isEmpty ());
80+ Assert .assertFalse ("Log ls" , lsResult .isEmpty ());
7881 Map levelResult = ipfs .log .level ("all" , "info" );
7982 Assert .assertTrue ("Log level" , ((String )levelResult .get ("Message" )).startsWith ("Changed log level" ));
8083 }
@@ -89,7 +92,7 @@ public void ipldNode() {
8992
9093 IpldNode .CborIpldNode node = new IpldNode .CborIpldNode (cbor );
9194 List <String > tree = node .tree ("" , -1 );
92- Assert . assertTrue ("Correct tree" , tree . equals ( Arrays .asList ("/a/b" , "/c" ) ));
95+ assertEquals ("Correct tree" , tree , Arrays .asList ("/a/b" , "/c" ));
9396 }
9497
9598 @ Test
@@ -166,15 +169,17 @@ public void directoryTest() throws IOException {
166169 throw new IllegalStateException ("Different contents!" );
167170 }
168171
169- // @Test
172+ @ Ignore
173+ @ Test
170174 public void largeFileTest () throws IOException {
171175 byte [] largerData = new byte [100 *1024 *1024 ];
172176 new Random (1 ).nextBytes (largerData );
173177 NamedStreamable .ByteArrayWrapper largeFile = new NamedStreamable .ByteArrayWrapper ("nontrivial.txt" , largerData );
174178 fileTest (largeFile );
175179 }
176180
177- // @Test
181+ @ Ignore
182+ @ Test
178183 public void hugeFileStreamTest () throws IOException {
179184 byte [] hugeData = new byte [1000 *1024 *1024 ];
180185 new Random (1 ).nextBytes (hugeData );
@@ -243,7 +248,7 @@ public void filesTest() throws IOException {
243248 Map stat = ipfs .files .stat ( path );
244249 Map stat2 = ipfs .files .stat ( path , Optional .of ("<cumulsize>" ), true );
245250 String readContents = new String (ipfs .files .read (path ));
246- Assert . assertTrue ("Should be equals" , contents . equals ( readContents ) );
251+ assertEquals ("Should be equals" , contents , readContents );
247252 res = ipfs .files .rm (path , false , false );
248253
249254 String tempFilename = "temp.txt" ;
@@ -285,14 +290,14 @@ public void filesTest() throws IOException {
285290 @ Test
286291 public void multibaseTest () throws IOException {
287292 List <Map > encodings = ipfs .multibase .list (true , false );
288- Assert .assertTrue ("multibase/list works" , ! encodings .isEmpty ());
293+ Assert .assertFalse ("multibase/list works" , encodings .isEmpty ());
289294 String encoded = ipfs .multibase .encode (Optional .empty (), new NamedStreamable .ByteArrayWrapper ("hello" .getBytes ()));
290- Assert . assertTrue ("multibase/encode works" , encoded . equals ( "uaGVsbG8" ) );
295+ assertEquals ("multibase/encode works" , "uaGVsbG8" , encoded );
291296 String decoded = ipfs .multibase .decode (new NamedStreamable .ByteArrayWrapper (encoded .getBytes ()));
292- Assert . assertTrue ("multibase/decode works" , decoded . equals ( "hello" ) );
297+ assertEquals ("multibase/decode works" , "hello" , decoded );
293298 String input = "f68656c6c6f" ;
294299 String transcode = ipfs .multibase .transcode (Optional .of ("base64url" ), new NamedStreamable .ByteArrayWrapper (input .getBytes ()));
295- Assert . assertTrue ("multibase/transcode works" , transcode . equals ( encoded ) );
300+ assertEquals ("multibase/transcode works" , transcode , encoded );
296301 }
297302
298303 @ Test
@@ -460,8 +465,8 @@ public void objectTest() throws IOException {
460465 MerkleNode _new = ipfs .object ._new (Optional .empty ());
461466 Multihash pointer = Multihash .fromBase58 ("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB" );
462467 MerkleNode object = ipfs .object .get (pointer );
463- List <MerkleNode > newPointer = ipfs .object .put (Arrays . asList (object .toJSONString ().getBytes ()));
464- List <MerkleNode > newPointer2 = ipfs .object .put ("json" , Arrays . asList (object .toJSONString ().getBytes ()));
468+ List <MerkleNode > newPointer = ipfs .object .put (Collections . singletonList (object .toJSONString ().getBytes ()));
469+ List <MerkleNode > newPointer2 = ipfs .object .put ("json" , Collections . singletonList (object .toJSONString ().getBytes ()));
465470 MerkleNode links = ipfs .object .links (pointer );
466471 byte [] data = ipfs .object .data (pointer );
467472 Map stat = ipfs .object .stat (pointer );
@@ -472,7 +477,7 @@ public void blockTest() throws IOException {
472477 MerkleNode pointer = new MerkleNode ("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB" );
473478 Map stat = ipfs .block .stat (pointer .hash );
474479 byte [] object = ipfs .block .get (pointer .hash );
475- List <MerkleNode > newPointer = ipfs .block .put (Arrays . asList ("Some random data..." .getBytes ()));
480+ List <MerkleNode > newPointer = ipfs .block .put (Collections . singletonList ("Some random data..." .getBytes ()));
476481 }
477482
478483 @ Test
@@ -493,18 +498,18 @@ public void publish() throws Exception {
493498
494499 // Add a DAG node to IPFS
495500 MerkleNode merkleNode = ipfs .dag .put ("json" , json .getBytes ());
496- Assert . assertEquals ("expected to be bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , "bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , merkleNode .hash .toString ());
501+ assertEquals ("expected to be bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , "bafyreiafmbgul64c4nyybvgivswmkuhifamc24cdfuj4ij5xtnhpsfelky" , merkleNode .hash .toString ());
497502
498503 // Get a DAG node
499504 byte [] res = ipfs .dag .get ((Cid ) merkleNode .hash );
500- Assert . assertEquals ("Should be equals" , JSONParser .parse (json ), JSONParser .parse (new String (res )));
505+ assertEquals ("Should be equals" , JSONParser .parse (json ), JSONParser .parse (new String (res )));
501506
502507 // Publish to IPNS
503508 Map result = ipfs .name .publish (merkleNode .hash );
504509
505510 // Resolve from IPNS
506511 String resolved = ipfs .name .resolve (Cid .decode ((String ) result .get ("Name" )));
507- Assert . assertEquals ("Should be equals" , resolved , "/ipfs/" + merkleNode .hash . toString () );
512+ assertEquals ("Should be equals" , resolved , "/ipfs/" + merkleNode .hash );
508513 }
509514
510515 @ Test
@@ -536,7 +541,7 @@ public void pubsub() throws Exception {
536541 ipfs .pubsub .pub (topic , data );
537542 ipfs .pubsub .pub (topic , "G'day" );
538543 List <Map > results = sub .limit (2 ).collect (Collectors .toList ());
539- Assert .assertTrue ( ! results .get (0 ). equals ( Collections .emptyMap () ));
544+ Assert .assertNotEquals ( results .get (0 ), Collections .emptyMap ());
540545 }
541546
542547 private static String toEscapedHex (byte [] in ) throws IOException {
@@ -556,15 +561,15 @@ public void merkleLinkInMap() throws IOException {
556561 Random r = new Random ();
557562 CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" ).getBytes ());
558563 byte [] rawTarget = target .toByteArray ();
559- MerkleNode targetRes = ipfs .block .put (Arrays . asList (rawTarget ), Optional .of ("cbor" )).get (0 );
564+ MerkleNode targetRes = ipfs .block .put (Collections . singletonList (rawTarget ), Optional .of ("cbor" )).get (0 );
560565
561566 CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (targetRes .hash );
562567 Map <String , CborObject > m = new TreeMap <>();
563568 m .put ("alink" , link );
564569 m .put ("arr" , new CborObject .CborList (Collections .emptyList ()));
565570 CborObject .CborMap source = CborObject .CborMap .build (m );
566571 byte [] rawSource = source .toByteArray ();
567- MerkleNode sourceRes = ipfs .block .put (Arrays . asList (rawSource ), Optional .of ("cbor" )).get (0 );
572+ MerkleNode sourceRes = ipfs .block .put (Collections . singletonList (rawSource ), Optional .of ("cbor" )).get (0 );
568573
569574 CborObject .fromByteArray (rawSource );
570575
@@ -576,7 +581,7 @@ public void merkleLinkInMap() throws IOException {
576581 Assert .assertTrue ("refs returns links" , refs .contains (targetRes .hash ));
577582
578583 byte [] bytes = ipfs .block .get (targetRes .hash );
579- Assert . assertTrue ("same contents after GC" , Arrays . equals ( bytes , rawTarget ) );
584+ assertArrayEquals ("same contents after GC" , bytes , rawTarget );
580585 // These commands can be used to reproduce this on the command line
581586 String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
582587 String reproCommand2 = "printf \" " + toEscapedHex (rawSource ) + "\" | ipfs block put --format=cbor" ;
@@ -587,34 +592,34 @@ public void merkleLinkInMap() throws IOException {
587592 public void recursiveRefs () throws IOException {
588593 CborObject .CborByteArray leaf1 = new CborObject .CborByteArray (("G'day IPFS!" ).getBytes ());
589594 byte [] rawLeaf1 = leaf1 .toByteArray ();
590- MerkleNode leaf1Res = ipfs .block .put (Arrays . asList (rawLeaf1 ), Optional .of ("cbor" )).get (0 );
595+ MerkleNode leaf1Res = ipfs .block .put (Collections . singletonList (rawLeaf1 ), Optional .of ("cbor" )).get (0 );
591596
592597 CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (leaf1Res .hash );
593598 Map <String , CborObject > m = new TreeMap <>();
594599 m .put ("link1" , link );
595600 CborObject .CborMap source = CborObject .CborMap .build (m );
596- MerkleNode sourceRes = ipfs .block .put (Arrays . asList (source .toByteArray ()), Optional .of ("cbor" )).get (0 );
601+ MerkleNode sourceRes = ipfs .block .put (Collections . singletonList (source .toByteArray ()), Optional .of ("cbor" )).get (0 );
597602
598603 CborObject .CborByteArray leaf2 = new CborObject .CborByteArray (("G'day again, IPFS!" ).getBytes ());
599604 byte [] rawLeaf2 = leaf2 .toByteArray ();
600- MerkleNode leaf2Res = ipfs .block .put (Arrays . asList (rawLeaf2 ), Optional .of ("cbor" )).get (0 );
605+ MerkleNode leaf2Res = ipfs .block .put (Collections . singletonList (rawLeaf2 ), Optional .of ("cbor" )).get (0 );
601606
602607 Map <String , CborObject > m2 = new TreeMap <>();
603608 m2 .put ("link1" , new CborObject .CborMerkleLink (sourceRes .hash ));
604609 m2 .put ("link2" , new CborObject .CborMerkleLink (leaf2Res .hash ));
605610 CborObject .CborMap source2 = CborObject .CborMap .build (m2 );
606- MerkleNode rootRes = ipfs .block .put (Arrays . asList (source2 .toByteArray ()), Optional .of ("cbor" )).get (0 );
611+ MerkleNode rootRes = ipfs .block .put (Collections . singletonList (source2 .toByteArray ()), Optional .of ("cbor" )).get (0 );
607612
608613 List <Multihash > refs = ipfs .refs (rootRes .hash , false );
609614 boolean correct = refs .contains (sourceRes .hash ) && refs .contains (leaf2Res .hash ) && refs .size () == 2 ;
610615 Assert .assertTrue ("refs returns links" , correct );
611616
612617 List <Multihash > refsRecurse = ipfs .refs (rootRes .hash , true );
613- boolean correctRecurse = refs .contains (sourceRes .hash )
614- && refs .contains (leaf1Res .hash )
615- && refs .contains (leaf2Res .hash )
616- && refs .size () == 3 ;
617- Assert .assertTrue ("refs returns links" , correct );
618+ boolean correctRecurse = refsRecurse .contains (sourceRes .hash )
619+ && refsRecurse .contains (leaf1Res .hash )
620+ && refsRecurse .contains (leaf2Res .hash )
621+ && refsRecurse .size () == 3 ;
622+ Assert .assertTrue ("refs returns links" , correctRecurse );
618623 }
619624
620625 /**
@@ -625,23 +630,23 @@ public void rootMerkleLink() throws IOException {
625630 Random r = new Random ();
626631 CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r .nextInt ()).getBytes ());
627632 byte [] rawTarget = target .toByteArray ();
628- MerkleNode block1 = ipfs .block .put (Arrays . asList (rawTarget ), Optional .of ("cbor" )).get (0 );
633+ MerkleNode block1 = ipfs .block .put (Collections . singletonList (rawTarget ), Optional .of ("cbor" )).get (0 );
629634 Multihash block1Hash = block1 .hash ;
630635 byte [] retrievedObj1 = ipfs .block .get (block1Hash );
631- Assert . assertTrue ("get inverse of put" , Arrays . equals ( retrievedObj1 , rawTarget ) );
636+ assertArrayEquals ("get inverse of put" , retrievedObj1 , rawTarget );
632637
633638 CborObject .CborMerkleLink cbor2 = new CborObject .CborMerkleLink (block1 .hash );
634639 byte [] obj2 = cbor2 .toByteArray ();
635- MerkleNode block2 = ipfs .block .put (Arrays . asList (obj2 ), Optional .of ("cbor" )).get (0 );
640+ MerkleNode block2 = ipfs .block .put (Collections . singletonList (obj2 ), Optional .of ("cbor" )).get (0 );
636641 byte [] retrievedObj2 = ipfs .block .get (block2 .hash );
637- Assert . assertTrue ("get inverse of put" , Arrays . equals ( retrievedObj2 , obj2 ) );
642+ assertArrayEquals ("get inverse of put" , retrievedObj2 , obj2 );
638643
639644 List <Multihash > add = ipfs .pin .add (block2 .hash );
640645 ipfs .repo .gc ();
641646 ipfs .repo .gc ();
642647
643648 byte [] bytes = ipfs .block .get (block1 .hash );
644- Assert . assertTrue ("same contents after GC" , Arrays . equals ( bytes , rawTarget ) );
649+ assertArrayEquals ("same contents after GC" , bytes , rawTarget );
645650 // These commands can be used to reproduce this on the command line
646651 String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
647652 String reproCommand2 = "printf \" " + toEscapedHex (obj2 ) + "\" | ipfs block put --format=cbor" ;
@@ -655,9 +660,9 @@ public void rootMerkleLink() throws IOException {
655660 public void rootNull () throws IOException {
656661 CborObject .CborNull cbor = new CborObject .CborNull ();
657662 byte [] obj = cbor .toByteArray ();
658- MerkleNode block = ipfs .block .put (Arrays . asList (obj ), Optional .of ("cbor" )).get (0 );
663+ MerkleNode block = ipfs .block .put (Collections . singletonList (obj ), Optional .of ("cbor" )).get (0 );
659664 byte [] retrievedObj = ipfs .block .get (block .hash );
660- Assert . assertTrue ("get inverse of put" , Arrays . equals ( retrievedObj , obj ) );
665+ assertArrayEquals ("get inverse of put" , retrievedObj , obj );
661666
662667 List <Multihash > add = ipfs .pin .add (block .hash );
663668 ipfs .repo .gc ();
@@ -676,19 +681,19 @@ public void merkleLinkInList() throws IOException {
676681 Random r = new Random ();
677682 CborObject .CborByteArray target = new CborObject .CborByteArray (("g'day IPFS!" + r .nextInt ()).getBytes ());
678683 byte [] rawTarget = target .toByteArray ();
679- MerkleNode targetRes = ipfs .block .put (Arrays . asList (rawTarget ), Optional .of ("cbor" )).get (0 );
684+ MerkleNode targetRes = ipfs .block .put (Collections . singletonList (rawTarget ), Optional .of ("cbor" )).get (0 );
680685
681686 CborObject .CborMerkleLink link = new CborObject .CborMerkleLink (targetRes .hash );
682- CborObject .CborList source = new CborObject .CborList (Arrays . asList (link ));
687+ CborObject .CborList source = new CborObject .CborList (Collections . singletonList (link ));
683688 byte [] rawSource = source .toByteArray ();
684- MerkleNode sourceRes = ipfs .block .put (Arrays . asList (rawSource ), Optional .of ("cbor" )).get (0 );
689+ MerkleNode sourceRes = ipfs .block .put (Collections . singletonList (rawSource ), Optional .of ("cbor" )).get (0 );
685690
686691 List <Multihash > add = ipfs .pin .add (sourceRes .hash );
687692 ipfs .repo .gc ();
688693 ipfs .repo .gc ();
689694
690695 byte [] bytes = ipfs .block .get (targetRes .hash );
691- Assert . assertTrue ("same contents after GC" , Arrays . equals ( bytes , rawTarget ) );
696+ assertArrayEquals ("same contents after GC" , bytes , rawTarget );
692697 // These commands can be used to reproduce this on the command line
693698 String reproCommand1 = "printf \" " + toEscapedHex (rawTarget ) + "\" | ipfs block put --format=cbor" ;
694699 String reproCommand2 = "printf \" " + toEscapedHex (rawSource ) + "\" | ipfs block put --format=cbor" ;
@@ -819,12 +824,12 @@ public void swarmTestFilters() throws IOException {
819824 Map rm = ipfs .swarm .rmFilter (multiAddrFilter );
820825 Map filters = ipfs .swarm .filters ();
821826 List <String > filtersList = (List <String >)filters .get ("Strings" );
822- Assert .assertTrue ("Filters empty" , filtersList == null );
827+ Assert .assertNull ("Filters empty" , filtersList );
823828
824829 Map added = ipfs .swarm .addFilter (multiAddrFilter );
825830 filters = ipfs .swarm .filters ();
826831 filtersList = (List <String >)filters .get ("Strings" );
827- Assert .assertTrue ("Filters NOT empty" , ! filtersList .isEmpty ());
832+ Assert .assertFalse ("Filters NOT empty" , filtersList .isEmpty ());
828833 rm = ipfs .swarm .rmFilter (multiAddrFilter );
829834 }
830835
@@ -838,7 +843,7 @@ public void swarmTestPeering() throws IOException {
838843 Map addPeering = ipfs .swarm .addPeering (ma );
839844 Map lsPeering = ipfs .swarm .lsPeering ();
840845 List <String > peeringList = (List <String >)lsPeering .get ("Peers" );
841- Assert .assertTrue ("Filters not empty" , ! peeringList .isEmpty ());
846+ Assert .assertFalse ("Filters not empty" , peeringList .isEmpty ());
842847 Map rmPeering = ipfs .swarm .rmPeering (hash );
843848 lsPeering = ipfs .swarm .lsPeering ();
844849 peeringList = (List <String >)lsPeering .get ("Peers" );
@@ -925,9 +930,9 @@ public void addArgsTest() {
925930 .setCidVersion (1 )
926931 .build ();
927932 String res = args .toString ();
928- Assert . assertTrue ("args toString() format" , res . equals ( "[cid-version = 1, inline = true]" ) );
933+ assertEquals ("args toString() format" , "[cid-version = 1, inline = true]" , res );
929934 String queryStr = args .toQueryString ();
930- Assert . assertTrue ("args toQueryString() format" , queryStr . equals ( "inline=true&cid-version=1" ) );
935+ assertEquals ("args toQueryString() format" , "inline=true&cid-version=1" , queryStr );
931936 }
932937
933938 // this api is disabled until deployment over IPFS is enabled
0 commit comments