package org.dainst.chronontology;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.dainst.chronontology.handler.model.Document;
import org.dainst.chronontology.handler.model.Results;
import org.dainst.chronontology.util.JsonUtils;
import org.json.JSONException;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompare;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.JSONCompareResult;

import java.io.IOException;
import java.util.List;

import static org.dainst.chronontology.util.JsonUtils.json;
import static org.testng.Assert.fail;

/**
 * @author Daniel M. de Oliveira
 */
public class JsonTestUtils {

    public static void jsonAssertEquals(final JsonNode actual,final JsonNode expected) {
        try {
            JSONAssert.assertEquals(
                    expected.toString(),
                    actual.toString(), false);
        } catch (JSONException e) {
            fail(e.getMessage());
        }
    }

    public static JsonNode sampleDocument(final String sampleFieldValue) {
        return sampleDocument(sampleFieldValue,null,null);
    }

    public static JsonNode sampleDocument(final String sampleFieldValue, String id) {
        return sampleDocument(sampleFieldValue,id,null);
    }

    public static JsonNode sampleDocument(final String sampleFieldValue, String id, String dataset) {
        JsonNode json= null;
        json = JsonUtils.json();
        ((ObjectNode)json).put(Document.RESOURCE,sampleFieldValue);
        if (id!=null)
            ((ObjectNode)json).put(Document.ID,id);
        if (dataset!=null)
            ((ObjectNode)json).put(Document.DATASET,dataset);
        return json;
    }

    private static Results results() {
        return new Results("results");
    }

    public static void assertResultsAreFound(JsonNode searchResult, List<String> ids)  {

        Results expected= results();
        for (String id:ids) {
            try {
                expected.add(json("{\"@id\" : \""+id+"\"}"));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }

        try {
            JSONCompareResult r  = null;
            r = JSONCompare.compareJSON(
                    expected.toString(),
                    searchResult.toString(), JSONCompareMode.LENIENT);

            if (r.failed()) fail(r.getMessage());
        } catch (JSONException e) {
            fail(e.getMessage());
        }
    }
}

@@ -1837,88 +1837,765 @@
     
-private static Results results() %7B%0A        return new Results(%22r
+/**%0A     * Takes a %3Ccode%3EsearchResult%3C/code%3E as obtained from the search endpoint and%0A     * tests if it contains results for any of the specified ids.%0A     *%0A     * For example, if %3Ccode%3Eids%3C/code%3E is %7B1,2%7D, %3Ccode%3EsearchResult%3C/code%3E set must contain%0A     * two nodes with at least the @id fields. All other fields are optional, but%0A     * it must contain the two nodes.%0A     *%0A     * %3Cpre%3E%0A     * %7B%0A     *     %22results%22 : %5B%0A     *          %7B%0A     *              %22@id%22 : %221%22%0A     *          %7D,%0A     *          %7B%0A     *              %22@id%22 : %222%22%0A     *          %7D%0A     *     %5D%0A     * %7D%0A     * %3C/pre%3E%0A     *%0A     * If there is at least one node missing, the method fails with%0A     * testng's fail().%0A     *%0A     * @param searchR
 esult
-s%22);
 %0A    
-%7D%0A
+ * @param ids%0A     */
 %0A   
@@ -2711,16 +2711,29 @@
 ed= 
-r
+new R
 esults(
+%22results%22
 );%0A 
@@ -2817,19 +2817,31 @@
 son(%22%7B%5C%22
-@id
+%22+Document.ID+%22
 %5C%22 : %5C%22%22

Add some apidoc.