package com.riaektiv.crypto.tree

import org.junit.Assert.assertEquals
import org.junit.Test
import java.math.BigDecimal

/**
 * Coding With Passion Since 1991
 * Created: 7/10/2016, 8:24 AM Eastern Time

 * @author Sergey Chuykov
 */
class AccountTreeTest {

    var tree = AccountTreeFactory().tree()

    lateinit var account: Account

    fun bd(value: Double): BigDecimal
            = BigDecimal.valueOf(value)

    fun account(number: String, balance: Double)
            = tree.entity(number, bd(balance))

    @Test
    fun message() {

        account = account("1", 0.00)
        assertEquals("1:0.00", tree.message(account))
        assertEquals("ce9d02a86390870f09658d8c46ad3805671f7ff06d73f6e17e35e00e", account.hash())

        account = account("3", 17.03)
        assertEquals("3:17.03", tree.message(account))
        assertEquals("e6c59ea5a948ae0856537d5a0da10dd8d254d7b887c2906d76ad6cfc", account.hash())

    }

    @Test
    fun putGetValidate() {

        tree.validate()

        val accounts = arrayOf(
                account("1", 101.01),
                account("3", 303.03),
                account("77A9F", 777.77)
        )

        for (i in 0..2) {
            tree.put(accounts[i])
            tree.validate()
        }

        val expected = arrayOf(
                account("1", 101.01),
                account("3", 303.03),
                account("77A9F", 777.77)
        )

        for (i in 0..2) {
            assertEquals(expected[i], tree.get(accounts[i].number))
        }

    }

}

