package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.swig.create_torrent;
import com.frostwire.jlibtorrent.swig.entry;
import com.frostwire.jlibtorrent.swig.entry_list;
import com.frostwire.jlibtorrent.swig.string_entry_map;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * @author gubatron
 * @author aldenml
 */
public class ChangeTrackersTest {

    @Test
    public void testChangeTrackersUsingCreateTorrent() throws IOException {
        byte[] torrentBytes = Utils.getResourceBytes("test4.torrent");
        TorrentInfo ti = TorrentInfo.bdecode(torrentBytes);

        // do we have any tracker
        assertTrue(ti.trackers().size() > 0);

        entry e = entry.bdecode(Vectors.bytes2byte_vector(torrentBytes));
        string_entry_map m = e.dict();
        if (m.has_key("announce")) {
            m.del("announce");
        }
        if (m.has_key("announce-list")) {
            m.del("announce-list");
        }

        ti = TorrentInfo.bdecode(Vectors.byte_vector2bytes(e.bencode()));
        // did we remove all trackers
        assertEquals(ti.trackers().size(), 0);

        create_torrent c = new create_torrent(ti.getSwig());

        c.add_tracker("http://a:6969/announce", 0);
        c.add_tracker("http://b:6969/announce", 1);

        e = c.generate();
        ti = TorrentInfo.bdecode(Vectors.byte_vector2bytes(e.bencode()));
        List<AnnounceEntry> trackers = ti.trackers();
        // do we have exactly the two added trackers
        assertEquals(trackers.size(), 2);
        assertEquals(trackers.get(0).getUrl(), "http://a:6969/announce");
        assertEquals(trackers.get(1).getUrl(), "http://b:6969/announce");
    }

    @Test
    public void testChangeTrackersLowLevel() throws IOException {
        byte[] torrentBytes = Utils.getResourceBytes("test4.torrent");
        TorrentInfo ti = TorrentInfo.bdecode(torrentBytes);

        // do we have any tracker
        assertTrue(ti.trackers().size() > 0);

        entry e = entry.bdecode(Vectors.bytes2byte_vector(torrentBytes));
        string_entry_map m = e.dict();

        // remove trackers
        if (m.has_key("announce")) {
            m.del("announce");
        }
        if (m.has_key("announce-list")) {
            m.del("announce-list");
        }

        // add trackers
        String[] tks = new String[]{"http://a:6969/announce", "http://b:6969/announce"};
        entry_list l = new entry_list();
        l.push_back(new entry(tks[0]));
        m.set("announce", new entry(l));

        entry_list tl = new entry_list();
        for (int i = 0; i < tks.length; i++) {
            l.clear();
            l.push_back(new entry(tks[i]));
            tl.push_back(new entry(l));
        }
        m.set("announce-list", new entry(tl));

        ti = TorrentInfo.bdecode(Vectors.byte_vector2bytes(e.bencode()));
        List<AnnounceEntry> trackers = ti.trackers();
        // do we have exactly the two added trackers
        assertEquals(trackers.size(), 2);
        assertEquals(trackers.get(0).getUrl(), "http://a:6969/announce");
        assertEquals(trackers.get(1).getUrl(), "http://b:6969/announce");
    }
}

@@ -3254,11 +3254,844 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testChangeTrackersWithTorrentInfo() throws IOException %7B%0A        byte%5B%5D torrentBytes = Utils.getResourceBytes(%22test4.torrent%22);%0A        TorrentInfo ti = TorrentInfo.bdecode(torrentBytes);%0A%0A        // do we have any tracker%0A        assertTrue(ti.trackers().size() %3E 0);%0A%0A        ti.getSwig().trackers().clear();%0A%0A        // did we remove all trackers%0A        assertEquals(ti.trackers().size(), 0);%0A%0A        ti.addTracker(%22http://a:6969/announce%22, 0);%0A        ti.addTracker(%22http://b:6969/announce%22, 1);%0A%0A        List%3CAnnounceEntry%3E trackers = ti.trackers();%0A        // do we have exactly the two added trackers%0A        assertEquals(trackers.size(), 2);%0A        assertEquals(trackers.get(0).getUrl(), %22http://a:6969/announce%22);%0A        assertEquals(trackers.get(1).getUrl(), %22http://b:6969/announce%22);%0A    %7D
 %0A%7D%0A

Test for change trackers using only TorrentInfo