package org.nohope.typetools.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.awt.*;
import java.io.IOException;

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

/**
 * Date: 11/8/12
 * Time: 3:28 PM
 */
public class ColorModuleTest {
    @Test
    public void testColorSerialization() throws Exception {
        final Color color = new Color(10, 20, 30, 40);

        final ObjectMapper usualMapper = getMapper();
        usualMapper.enableDefaultTypingAsProperty(ObjectMapper.DefaultTyping.NON_FINAL, "@class");

        final String colorSerialized = usualMapper.writeValueAsString(color);
        final Color c = usualMapper.readValue(colorSerialized, Color.class);

        assertEquals(color, c);
        assertEquals(color.getAlpha(), c.getAlpha());
    }

    @Test
    public void invalidColourDeserialization() {
        final ObjectMapper mapper = getMapper();

        try {
            mapper.readValue("[0.039215688,0.078431375,0.11764706,0.15686275, 1]", Color.class);
            fail();
        } catch (IOException ignored) {
        }

        try {
            mapper.readValue("{}", Color.class);
            fail();
        } catch (IOException ignored) {
        }
    }

    private static ObjectMapper getMapper() {
        final ObjectMapper usualMapper = new ObjectMapper();
        usualMapper.registerModule(new ColorModule());

        return usualMapper;
    }
}

@@ -81,16 +81,77 @@
 Mapper;%0A
+import com.fasterxml.jackson.databind.jsonschema.JsonSchema;%0A
 import o
@@ -891,16 +891,169 @@
 lpha());
+%0A%0A        final JsonSchema jsonSchema = usualMapper.generateJsonSchema(Color.class);%0A        assertEquals(%22%7B%5C%22type%5C%22:%5C%22array%5C%22%7D%22, jsonSchema.toString());
 %0A    %7D%0A%0A

test for json schema