package com.github.javadev.nazk.client;

import java.util.List;
import java.util.Map;
import com.github.underscore.lodash.$;
import org.junit.Test;

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

public class HttpClientTest {
    private static NazkClient client = HttpClient.createDefault();

    @Test
    public void getDeclarations() {
        Map<String, Object> declarations = client.getDeclarations("");
        assertEquals("1", $.get(declarations, "page.currentPage").toString());
        assertEquals("400", $.get(declarations, "page.batchSize").toString());
    }

    @Test
    public void getDeclaration() {
        Map<String, Object> declaration = client.getDeclaration("043c6b5d-a470-4fb0-bc3b-3332af7fe10e");
        assertEquals("1", $.get(declaration, "data.step_0.declarationType").toString());
        assertEquals("Люстдорфська дорога", $.get(declaration, "data.step_1.street").toString());
    }

    @Test
    public void getDeclarationHtml() {
        String declaration = client.getDeclarationHtml("043c6b5d-a470-4fb0-bc3b-3332af7fe10e");
        assertTrue(declaration.startsWith("<!DOCTYPE html><html lang=\"en\"><head> "
            + "<meta charset=\"utf-8\"> <title>Перегляд декларації"));
    }

    @Test
    public void getDeclarationPdf() {
        byte[] data = $.fetch("https://public.nazk.gov.ua/storage/documents/pdf/"
            + "5/0/e/e/50ee7eae-f4ee-4b5c-b67d-9917154f7c53.pdf").blob();
        assertTrue(data.length > 10000);
    }

    @Test
    public void getDeclarationsBatch() {
        List<Map<String, Object>> declarationsBatch = client.getDeclarationsBatch(0);
        assertEquals(400, declarationsBatch.size());
    }

    @Test 
    public void getAndSaveAllDeclarations() {
        client.getAndSaveAllDeclarations("test", 1);
    }

    @Test
    public void main() {
        HttpClient.main("test");
    }
}

@@ -876,27 +876,34 @@
 ls(%22
-%D0%9B%D1%8E%D1%81%D1%82%D0%B4%D0%BE%D1%80%D1%84%D1%81%D1%8C%D0%BA%D0%B0 %D0%B4%D0%BE%D1%80%D0%BE%D0%B3%D0%B0
+%5B%D0%9A%D0%BE%D0%BD%D1%84%D1%96%D0%B4%D0%B5%D0%BD%D1%86%D1%96%D0%B9%D0%BD%D0%B0 %D1%96%D0%BD%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%86%D1%96%D1%8F%5D
 %22, $
@@ -1677,18 +1677,63 @@
 sert
-Equals(400
+True(%22declarationsBatch.size() should be more than 390%22
 , de
@@ -1754,16 +1754,22 @@
 h.size()
+ %3E 390
 );%0A    %7D

Improve unit tests.