package com.github.nsnjson;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.*;
import org.junit.*;

import static com.github.nsnjson.format.Format.*;

public class DriverTest extends AbstractFormatTest {

    @Test
    public void processTestNull() {
        shouldBeConsistencyWhenGivenNull(getNull(), getNullPresentation());
    }

    @Test
    public void processTestNumberInt() {
        NumericNode value = getNumberInt();

        shouldBeConsistencyWhenGivenNumber(value, getNumberIntPresentation(value));
    }

    @Test
    public void processTestNumberLong() {
        NumericNode value = getNumberLong();

        shouldBeConsistencyWhenGivenNumber(value, getNumberLongPresentation(value));
    }

    @Test
    public void processTestNumberDouble() {
        NumericNode value = getNumberDouble();

        shouldBeConsistencyWhenGivenNumber(value, getNumberDoublePresentation(value));
    }

    @Test
    public void processTestEmptyString() {
        TextNode value = getEmptyString();

        shouldBeConsistencyWhenGivenString(value, getStringPresentation(value));
    }

    @Test
    public void processTestString() {
        TextNode value = getString();

        shouldBeConsistencyWhenGivenString(value, getStringPresentation(value));
    }

    @Test
    public void processTestBooleanTrue() {
        BooleanNode value = getBooleanTrue();

        shouldBeConsistencyWhenGivenBoolean(value, getBooleanPresentation(value));
    }

    @Test
    public void processTestBooleanFalse() {
        BooleanNode value = getBooleanFalse();

        shouldBeConsistencyWhenGivenBoolean(value, getBooleanPresentation(value));
    }

    @Test
    public void processTestEmptyArray() {
        shouldBeConsistencyWhenGivenArray(getEmptyArray(), getEmptyArrayPresentation());
    }

    @Test
    public void processTestArray() {
        ObjectMapper objectMapper = new ObjectMapper();

        JsonNode numberInt = getNumberInt();

        JsonNode numberLong = getNumberLong();

        JsonNode numberDouble = getNumberDouble();

        JsonNode string = getString();

        JsonNode booleanTrue = getBooleanTrue();

        JsonNode booleanFalse = getBooleanFalse();

        ArrayNode array = objectMapper.createArrayNode();
        array.add(getNull());
        array.add(numberInt);
        array.add(numberLong);
        array.add(numberDouble);
        array.add(string);
        array.add(booleanTrue);
        array.add(booleanFalse);

        ObjectNode presentationOfNull = objectMapper.createObjectNode();
        presentationOfNull.put(FIELD_TYPE, TYPE_MARKER_NULL);

        ObjectNode presentationOfNumberInt = objectMapper.createObjectNode();
        presentationOfNumberInt.put(FIELD_TYPE, TYPE_MARKER_NUMBER);
        presentationOfNumberInt.put(FIELD_VALUE, numberInt.asInt());

        ObjectNode presentationOfNumberLong = objectMapper.createObjectNode();
        presentationOfNumberLong.put(FIELD_TYPE, TYPE_MARKER_NUMBER);
        presentationOfNumberLong.put(FIELD_VALUE, numberLong.asLong());

        ObjectNode presentationOfNumberDouble = objectMapper.createObjectNode();
        presentationOfNumberDouble.put(FIELD_TYPE, TYPE_MARKER_NUMBER);
        presentationOfNumberDouble.put(FIELD_VALUE, numberDouble.asDouble());

        ObjectNode presentationOfString = objectMapper.createObjectNode();
        presentationOfString.put(FIELD_TYPE, TYPE_MARKER_STRING);
        presentationOfString.put(FIELD_VALUE, string.asText());

        ObjectNode presentationOfBooleanTrue = objectMapper.createObjectNode();
        presentationOfBooleanTrue.put(FIELD_TYPE, TYPE_MARKER_BOOLEAN);
        presentationOfBooleanTrue.put(FIELD_VALUE, BOOLEAN_TRUE);

        ObjectNode presentationOfBooleanFalse = objectMapper.createObjectNode();
        presentationOfBooleanFalse.put(FIELD_TYPE, TYPE_MARKER_BOOLEAN);
        presentationOfBooleanFalse.put(FIELD_VALUE, BOOLEAN_FALSE);

        ArrayNode presentationOfArrayItems = objectMapper.createArrayNode();
        presentationOfArrayItems.add(presentationOfNull);
        presentationOfArrayItems.add(presentationOfNumberInt);
        presentationOfArrayItems.add(presentationOfNumberLong);
        presentationOfArrayItems.add(presentationOfNumberDouble);
        presentationOfArrayItems.add(presentationOfString);
        presentationOfArrayItems.add(presentationOfBooleanTrue);
        presentationOfArrayItems.add(presentationOfBooleanFalse);

        ObjectNode presentation = objectMapper.createObjectNode();
        presentation.put(FIELD_TYPE, TYPE_MARKER_ARRAY);
        presentation.set(FIELD_VALUE, presentationOfArrayItems);

        shouldBeConsistencyWhenGivenArray(array, presentation);
    }

    @Test
    public void processTestEmptyObject() {
        shouldBeConsistencyWhenGivenObject(getEmptyObject(), getEmptyObjectPresentation());
    }

    @Test
    public void processObject() {
        ObjectNode object = getObject();

        shouldBeConsistencyWhenGivenObject(object, getObjectPresentation(object));
    }

    private void shouldBeConsistencyWhenGivenNull(NullNode value, ObjectNode presentation) {
        assertConsistency(value, presentation);
    }

    private void shouldBeConsistencyWhenGivenNumber(NumericNode value, ObjectNode presentation) {
        assertConsistency(value, presentation);
    }

    private void shouldBeConsistencyWhenGivenString(TextNode value, ObjectNode presentation) {
        assertConsistency(value, presentation);
    }

    private void shouldBeConsistencyWhenGivenBoolean(BooleanNode value, ObjectNode presentation) {
        assertConsistency(value, presentation);
    }

    private void shouldBeConsistencyWhenGivenArray(ArrayNode array, ObjectNode presentation) {
        assertConsistency(array, presentation);
    }

    private  void shouldBeConsistencyWhenGivenObject(ObjectNode object, ObjectNode presentation) {
        assertConsistency(object, presentation);
    }

    private static void assertConsistency(JsonNode value, ObjectNode presentation) {
        assertConsistencyByEncoding(value);

        assertConsistencyByDecoding(presentation);
    }

    private static void assertConsistencyByEncoding(JsonNode value) {
        Assert.assertEquals(value, Driver.decode(Driver.encode(value)));
    }

    private static void assertConsistencyByDecoding(ObjectNode presentation) {
        Assert.assertEquals(presentation, Driver.encode(Driver.decode(presentation)));
    }

}
@@ -60,17 +60,24 @@
 atabind.
-*
+JsonNode
 ;%0Aimport
@@ -141,59 +141,8 @@
 *;%0A%0A
-import static com.github.nsnjson.format.Format.*;%0A%0A
 publ
@@ -1842,2846 +1842,115 @@
     
-ObjectMapper objectMapper = new ObjectMapper();%0A%0A        JsonNode numberInt = getNumberInt();%0A%0A        JsonNode numberLong = getNumberLong();%0A%0A        JsonNode numberDouble = getNumberDouble();%0A%0A        JsonNode string = getString();%0A%0A        JsonNode booleanTrue = getBooleanTrue();%0A%0A        JsonNode booleanFalse = getBooleanFalse();%0A%0A        ArrayNode array = objectMapper.createArrayNode();%0A        array.add(getNull());%0A        array.add(numberInt);%0A        array.add(numberLong);%0A        array.add(numberDouble);%0A        array.add(string);%0A        array.add(booleanTrue);%0A        array.add(booleanFalse);%0A%0A        ObjectNode presentationOfNull = objectMapper.createObjectNode();%0A        presentationOfNull.put(FIELD_TYPE, TYPE_MARKER_NULL);%0A%0A        ObjectNode presentationOfNumberInt = objectMapper.createObjectNode();%0A        presentationOfNumberInt.put(FIELD_TYPE, TYPE_MARKER_NUMBER);%0A        presentationOfNumberInt.put(FIELD_VALUE, numberInt.asInt());%0A%0A        ObjectNode presentationOfNumberLong = objectMapper.createObjectNode();%0A        presentationOfNumberLong.put(FIELD_TYPE, TYPE_MARKER_NUMBER);%0A        presentationOfNumberLong.put(FIELD_VALUE, numberLong.asLong());%0A%0A        ObjectNode presentationOfNumberDouble = objectMapper.createObjectNode();%0A        presentationOfNumberDouble.put(FIELD_TYPE, TYPE_MARKER_NUMBER);%0A        presentationOfNumberDouble.put(FIELD_VALUE, numberDouble.asDouble());%0A%0A        ObjectNode presentationOfString = objectMapper.createObjectNode();%0A        presentationOfString.put(FIELD_TYPE, TYPE_MARKER_STRING);%0A        presentationOfString.put(FIELD_VALUE, string.asText());%0A%0A        ObjectNode presentationOfBooleanTrue = objectMapper.createObjectNode();%0A        presentationOfBooleanTrue.put(FIELD_TYPE, TYPE_MARKER_BOOLEAN);%0A        presentationOfBooleanTrue.put(FIELD_VALUE, BOOLEAN_TRUE);%0A%0A        ObjectNode presentationOfBooleanFalse = objectMapper.createObjectNode();%0A        presentationOfBooleanFalse.put(FIELD_TYPE, TYPE_MARKER_BOOLEAN);%0A        presentationOfBooleanFalse.put(FIELD_VALUE, BOOLEAN_FALSE);%0A%0A        ArrayNode presentationOfArrayItems = objectMapper.createArrayNode();%0A        presentationOfArrayItems.add(presentationOfNull);%0A        presentationOfArrayItems.add(presentationOfNumberInt);%0A        presentationOfArrayItems.add(presentationOfNumberLong);%0A        presentationOfArrayItems.add(presentationOfNumberDouble);%0A        presentationOfArrayItems.add(presentationOfString);%0A        presentationOfArrayItems.add(presentationOfBooleanTrue);%0A        presentationOfArrayItems.add(presentationOfBooleanFalse);%0A%0A        ObjectNode presentation = objectMapper.createObjectNode();%0A        presentation.put(FIELD_TYPE, TYPE_MARKER_ARRAY);%0A        presentation.set(FIELD_VALUE, presentationOfArrayItems);%0A%0A        shouldBeConsistencyWhenGivenArray(array, presentation
+ArrayNode array = getArray();%0A%0A        shouldBeConsistencyWhenGivenArray(array, getArrayPresentation(array)
 );%0A 

Refactor method { DriverTest @ processTestArray }