package io.ipfs.api;

import java.io.*;
import java.nio.file.*;
import java.util.*;

import org.junit.Assert;
import org.junit.Test;

import io.ipfs.multiaddr.MultiAddress;

public class RecursiveAddTest {

    private final IPFS ipfs = new IPFS(new MultiAddress("/ip4/127.0.0.1/tcp/5001"));
    
    @Test
    public void testAdd() throws Exception {
        System.out.println("ipfs version: " + ipfs.version());

        String EXPECTED = "QmX5fZ6aUxNTAS7ZfYc8f4wPoMx6LctuNbMjuJZ9EmUSr6";

        Path base = Paths.get("tmpdata");
        base.toFile().mkdirs();
        Files.write(base.resolve("index.html"), "<html></html>".getBytes());
        Path js = base.resolve("js");
        js.toFile().mkdirs();
        Files.write(js.resolve("func.js"), "function() {console.log('Hey');}".getBytes());

        List<MerkleNode> add = ipfs.add(new NamedStreamable.FileWrapper(base.toFile()));
        MerkleNode node = add.get(add.size() - 1);
        Assert.assertEquals(EXPECTED, node.hash.toBase58());
    }

    @Test
    public void binaryRecursiveAdd() throws Exception {
        String EXPECTED = "Qmd1dTx4Z1PHxSHDR9jYoyLJTrYsAau7zLPE3kqo14s84d";

        Path base = Paths.get("tmpbindata");
        base.toFile().mkdirs();
        byte[] bindata = new byte[1024*1024];
        new Random(28).nextBytes(bindata);
        Files.write(base.resolve("data.bin"), bindata);
        Path js = base.resolve("js");
        js.toFile().mkdirs();
        Files.write(js.resolve("func.js"), "function() {console.log('Hey');}".getBytes());

        List<MerkleNode> add = ipfs.add(new NamedStreamable.FileWrapper(base.toFile()));
        MerkleNode node = add.get(add.size() - 1);
        Assert.assertEquals(EXPECTED, node.hash.toBase58());
    }

    @Test
    public void largeBinaryRecursiveAdd() throws Exception {
        String EXPECTED = "QmZdfdj7nfxE68fBPUWAGrffGL3sYGx1MDEozMg73uD2wj";

        Path base = Paths.get("tmplargebindata");
        base.toFile().mkdirs();
        byte[] bindata = new byte[100 * 1024*1024];
        new Random(28).nextBytes(bindata);
        Files.write(base.resolve("data.bin"), bindata);
        new Random(496).nextBytes(bindata);
        Files.write(base.resolve("data2.bin"), bindata);
        Path js = base.resolve("js");
        js.toFile().mkdirs();
        Files.write(js.resolve("func.js"), "function() {console.log('Hey');}".getBytes());

        List<MerkleNode> add = ipfs.add(new NamedStreamable.FileWrapper(base.toFile()));
        MerkleNode node = add.get(add.size() - 1);
        Assert.assertEquals(EXPECTED, node.hash.toBase58());
    }
}

@@ -2372,32 +2372,985 @@
 %22.getBytes());%0A%0A
+        List%3CMerkleNode%3E add = ipfs.add(new NamedStreamable.FileWrapper(base.toFile()));%0A        MerkleNode node = add.get(add.size() - 1);%0A        Assert.assertEquals(EXPECTED, node.hash.toBase58());%0A    %7D%0A%0A    @Test%0A    public void largeBinaryInSubdirRecursiveAdd() throws Exception %7B%0A        String EXPECTED = %22QmUYuMwCpgaxJhNxRA5Pmje8EfpEgU3eQSB9t3VngbxYJk%22;%0A%0A        Path base = Paths.get(%22tmplargebininsubdirdata%22);%0A        base.toFile().mkdirs();%0A        Path bindir = base.resolve(%22moredata%22);%0A        bindir.toFile().mkdirs();%0A        byte%5B%5D bindata = new byte%5B100 * 1024*1024%5D;%0A        new Random(28).nextBytes(bindata);%0A        Files.write(bindir.resolve(%22data.bin%22), bindata);%0A        new Random(496).nextBytes(bindata);%0A        Files.write(bindir.resolve(%22data2.bin%22), bindata);%0A%0A        Path js = base.resolve(%22js%22);%0A        js.toFile().mkdirs();%0A        Files.write(js.resolve(%22func.js%22), %22function() %7Bconsole.log('Hey');%7D%22.getBytes());%0A%0A
         List%3CMer

add test for larger binary data in a subdir in recursive add