/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.oak.plugins.document.rdb;

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

import java.io.UnsupportedEncodingException;
import java.util.Collections;

import org.apache.jackrabbit.oak.plugins.document.Collection;
import org.apache.jackrabbit.oak.plugins.document.DocumentStore;
import org.apache.jackrabbit.oak.plugins.document.DocumentStoreException;
import org.apache.jackrabbit.oak.plugins.document.DocumentStoreFixture;
import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class RDBDocumentSerializerTest  {

    private DocumentStoreFixture fixture = DocumentStoreFixture.RDB_H2;
    private DocumentStore store;
    private RDBDocumentSerializer ser;

    @Before
    public void setUp() throws Exception {
        store = fixture.createDocumentStore();
        ser = new RDBDocumentSerializer(store, Collections.singleton("_id"));
    }

    @After
    public void tearDown() throws Exception {
        fixture.dispose();
    }

    @Test
    public void testSimpleString() {
        RDBRow row = new RDBRow("_foo", true, 1, 2, 3, "{}", null);
        NodeDocument doc = this.ser.fromRow(Collection.NODES, row);
        assertEquals("_foo", doc.getId());
        assertEquals(true, doc.hasBinary());
        assertEquals(2L, doc.getModCount());
    }

    @Test
    public void testSimpleBlob() throws UnsupportedEncodingException {
        RDBRow row = new RDBRow("_foo", false, 1, 2, 3, "\"blob\"", "{}".getBytes("UTF-8"));
        NodeDocument doc = this.ser.fromRow(Collection.NODES, row);
        assertEquals("_foo", doc.getId());
        assertEquals(false, doc.hasBinary());
        assertEquals(2L, doc.getModCount());
    }

    @Test
    public void testSimpleBoth() throws UnsupportedEncodingException {
        try {
            RDBRow row = new RDBRow("_foo", true, 1, 2, 3, "{}", "{}".getBytes("UTF-8"));
            this.ser.fromRow(Collection.NODES, row);
            fail("should fail");
        }
        catch (DocumentStoreException expected) {
        }
    }

    @Test
    public void testBlobAndDiff() throws UnsupportedEncodingException {
        RDBRow row = new RDBRow("_foo", true, 1, 2, 3, "[[\"=\", \"foo\", \"bar\"],[\"M\", \"m1\", 1],[\"M\", \"m2\", 3]]", "{\"m1\":2, \"m2\":2}".getBytes("UTF-8"));
        NodeDocument doc = this.ser.fromRow(Collection.NODES, row);
        assertEquals("bar", doc.get("foo"));
        assertEquals(2L, doc.get("m1"));
        assertEquals(3L, doc.get("m2"));
    }

    @Test
    public void testBlobAndDiffBorked() throws UnsupportedEncodingException {
        try {
            RDBRow row = new RDBRow("_foo", true, 1, 2, 3, "[[\"\", \"\", \"\"]]", "{}".getBytes("UTF-8"));
            this.ser.fromRow(Collection.NODES, row);
            fail("should fail");
        }
        catch (DocumentStoreException expected) {
        }
    }

    @Test
    public void testSimpleStringNonAscii() {
        RDBRow row = new RDBRow("_foo", true, 1, 2, 3, "{\"x\":\"\u20ac\uD834\uDD1E\"}", null);
        NodeDocument doc = this.ser.fromRow(Collection.NODES, row);
        assertEquals("_foo", doc.getId());
        assertEquals("\u20ac\uD834\uDD1E", doc.get("x"));
    }
}

@@ -1393,24 +1393,49 @@
 nit.Before;%0A
+import org.junit.Ignore;%0A
 import org.j
@@ -3782,32 +3782,431 @@
        %7D%0A    %7D%0A%0A
+    @Ignore(%22known problem is json.simple%22)%0A    @Test%0A    public void testBrokenJSONTrailingComma() throws UnsupportedEncodingException %7B%0A        try %7B%0A            RDBRow row = new RDBRow(%22_foo%22, true, 1, 2, 3, %22%7B %5C%22x%5C%22 : 1, %7D%22, null);%0A            this.ser.fromRow(Collection.NODES, row);%0A            fail(%22should fail%22);%0A        %7D%0A        catch (DocumentStoreException expected) %7B%0A        %7D%0A    %7D%0A%0A
     @Test%0A    pu

add test for known json.simple issue (trailing comma)