// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package routing.demo.convert.test;

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

import org.junit.Test;

import routing.demo.convert.ConvertJSON2CSV;

public class TestJSON2CSV {
    @Test
    public void testSimple() {
        try {
            String json = "{ \"a\": 1, \"b\": \"xxx\"}";
            String csv = ConvertJSON2CSV.convert(json, ',', '"');
            assertEquals("xml", "1,\"xxx\"", csv);
        } catch (Exception e) {
            fail("Unexpected exception: " + e.getMessage());
        }
    }
    @Test
    public void testTricky() {
        try {
            String json = "{ \"a\": 1, \"b\": \"xxx\", \"c\": \"y,y\", \"d\": \"z\\\"z\" }";
            String csv = ConvertJSON2CSV.convert(json, ',', '"');
            assertEquals("xml", "1,\"xxx\",\"y,y\",\"z\"\"z\"", csv);
        } catch (Exception e) {
            fail("Unexpected exception: " + e.getMessage());
        }
    }
    @Test
    public void testNested() {
        try {
            String json = "{ \"a\": 1, \"b\": \"xxx\", \"c\": { \"d\":2, \"e\": \"yyy\"}}";
            String csv = ConvertJSON2CSV.convert(json, ',', '"');
            fail("Missing exception conerting from " + json + " to " + csv);
        } catch (Exception e) {
            // nothing
        }
    }
}
