package io.github.jonestimd.finance.swing.transaction;

import java.text.ParseException;
import java.util.stream.Stream;

import io.github.jonestimd.finance.domain.transaction.CategoryKey;
import io.github.jonestimd.finance.domain.transaction.TransactionCategory;
import org.junit.Test;

import static io.github.jonestimd.finance.swing.transaction.CategoryKeyFormat.*;
import static org.assertj.core.api.Assertions.*;

public class CategoryKeyFormatTest {
    public static final String PARENT_CODE = "parent";
    public static final String CHILD_CODE = "child";
    public static final String NESTED_CODE = PARENT_CODE + SEPARATOR + CHILD_CODE;

    @Test
    public void formatIgnoresNull() throws Exception {
        assertThat(new CategoryKeyFormat().format(null)).isEqualTo("");
    }

    @Test
    public void formatsSimpleCategory() throws Exception {
        CategoryKey key = new CategoryKey(null, "code");

        assertThat(new CategoryKeyFormat().format(key)).isEqualTo(key.getCode());
    }

    @Test
    public void formatsChildCategory() throws Exception {
        CategoryKey key = new CategoryKey(new TransactionCategory(PARENT_CODE), CHILD_CODE);

        assertThat(new CategoryKeyFormat().format(key)).isEqualTo(NESTED_CODE);

    }

    @Test(expected = ParseException.class)
    public void parseFailsForEmptyString() throws Exception {
        new CategoryKeyFormat().parseObject("");
    }

    @Test(expected = ParseException.class)
    public void parseFailsForWhiteSpace() throws Exception {
        new CategoryKeyFormat().parseObject(" ");
    }

    @Test(expected = ParseException.class)
    public void parseFailsForEmptyCodes() throws Exception {
        new CategoryKeyFormat().parseObject(SEPARATOR + SEPARATOR);
    }

    @Test
    public void parseCreatesCategory() throws Exception {
        CategoryKey key = new CategoryKeyFormat().parseObject("code");

        assertThat(key.getCode()).isEqualTo("code");
        assertThat(key.getParent()).isNull();
    }

    @Test
    public void parseCreatesNestedCategories() throws Exception {
        CategoryKey key = new CategoryKeyFormat().parseObject(NESTED_CODE);

        assertThat(key.getCode()).isEqualTo(CHILD_CODE);
        assertThat(key.getParent().getCode()).isEqualTo(PARENT_CODE);
        assertThat(key.getParent().getParent()).isNull();
    }

    @Test
    public void parseReusesCategory() throws Exception {
        TransactionCategory category = new TransactionCategory("code");

        CategoryKey key = new CategoryKeyFormat(Stream.of(category)).parseObject(category.getCode());

        assertThat(key).isSameAs(category.getKey());
    }

    @Test
    public void parseReusesNestedCategories() throws Exception {
        TransactionCategory parent = new TransactionCategory(PARENT_CODE);

        CategoryKey key = new CategoryKeyFormat(Stream.of(parent)).parseObject(NESTED_CODE);

        assertThat(key.getCode()).isEqualTo(CHILD_CODE);
        assertThat(key.getParent()).isSameAs(parent);
        assertThat(key.getParent().getParent()).isNull();
    }

    @Test
    public void parseTrimsCodes() throws Exception {
        TransactionCategory parent = new TransactionCategory(PARENT_CODE);

        CategoryKey key = new CategoryKeyFormat(Stream.of(parent)).parseObject(PARENT_CODE + " " + SEPARATOR + " " + CHILD_CODE + " ");

        assertThat(key.getCode()).isEqualTo(CHILD_CODE);
        assertThat(key.getParent()).isSameAs(parent);
        assertThat(key.getParent().getParent()).isNull();
    }
}
@@ -53,74 +53,8 @@
 n;%0A%0A
-import java.text.ParseException;%0Aimport java.util.stream.Stream;%0A%0A
 impo
@@ -116,16 +116,16 @@
 oryKey;%0A
+
 import i
@@ -1210,1134 +1210,72 @@
 d = 
-ParseException.class)%0A    public void parseFailsForEmptyString() throws Exception %7B%0A        new CategoryKeyFormat().parseObject(%22%22);%0A    %7D%0A%0A    @Test(expected = ParseException.class)%0A    public void parseFailsForWhiteSpace() throws Exception %7B%0A        new CategoryKeyFormat().parseObject(%22 %22);%0A    %7D%0A%0A    @Test(expected = ParseException.class)%0A    public void parseFailsForEmptyCodes() throws Exception %7B%0A        new CategoryKeyFormat().parseObject(SEPARATOR + SEPARATOR);%0A    %7D%0A%0A    @Test%0A    public void parseCreatesCategory() throws Exception %7B%0A        CategoryKey key = new CategoryKeyFormat().parseObject(%22code%22);%0A%0A        assertThat(key.getCode()).isEqualTo(%22code%22);%0A        assertThat(key.getParent()).isNull();%0A    %7D%0A%0A    @Test%0A    public void parseCreatesNestedCategories() throws Exception %7B%0A        CategoryKey key = new CategoryKeyFormat().parseObject(NESTED_CODE);%0A%0A        assertThat(key.getCode()).isEqualTo(CHILD_CODE);%0A        assertThat(key.getParent().getCode()).isEqualTo(PARENT_CODE);%0A        assertThat(key.getParent().getParent()).isNull();%0A    %7D%0A%0A    @Test%0A    public void parseReusesCategory() t
+UnsupportedOperationException.class)%0A    public void parseT
 hrows
- 
 Exce
@@ -1283,1110 +1283,80 @@
 tion
- %7B%0A        TransactionCategory category = new TransactionCategory(%22code%22);%0A%0A        CategoryKey key = new CategoryKeyFormat(Stream.of(category)).parseObject(category.getCode());%0A%0A        assertThat(key).isSameAs(category.getKey());%0A    %7D%0A%0A    @Test%0A    public void parseReusesNestedCategories() throws Exception %7B%0A        TransactionCategory parent = new TransactionCategory(PARENT_CODE);%0A%0A        CategoryKey key = new CategoryKeyFormat(Stream.of(parent)).parseObject(NESTED_CODE);%0A%0A        assertThat(key.getCode()).isEqualTo(CHILD_CODE);%0A        assertThat(key.getParent()).isSameAs(parent);%0A        assertThat(key.getParent().getParent()).isNull();%0A    %7D%0A%0A    @Test%0A    public void parseTrimsCodes() throws Exception %7B%0A        TransactionCategory parent = new TransactionCategory(PARENT_CODE);%0A%0A        CategoryKey key = new CategoryKeyFormat(Stream.of(parent)).parseObject(PARENT_CODE + %22 %22 + SEPARATOR + %22 %22 + CHILD_CODE + %22 %22);%0A%0A        assertThat(key.getCode()).isEqualTo(CHILD_CODE);%0A        assertThat(key.getParent()).isSameAs(parent);%0A        assertThat(key.getParent().getParent()).isNull(
+() throws Exception %7B%0A        new CategoryKeyFormat().parseObject(%22code%22
 );%0A 

Create a new transaction category from a transaction window