package com.github.jsonldjava.impl;

import static org.junit.Assert.assertTrue;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.github.jsonldjava.core.JSONLD;
import com.github.jsonldjava.core.JSONLDProcessingError;
import com.github.jsonldjava.core.JSONLDTripleCallback;
import com.github.jsonldjava.utils.JSONUtils;
import com.hp.hpl.jena.rdf.model.Model;

public class JenaTripleCallbackTest {

    @Test
    public void triplesTest() throws JsonParseException, JsonMappingException,
            JSONLDProcessingError {

        final List<Map<String, Object>> input = new ArrayList<Map<String, Object>>() {
            {
                add(new LinkedHashMap<String, Object>() {
                    {
                        put("@id", "http://localhost:8080/foo1");
                        put("http://foo.com/code", new ArrayList<Object>() {
                            {
                                add(new LinkedHashMap<String, Object>() {
                                    {
                                        put("@value", "123");
                                    }
                                });
                            }
                        });
                    }
                });
                add(new LinkedHashMap<String, Object>() {
                    {
                        put("@id", "http://localhost:8080/foo2");
                        put("http://foo.com/code", new ArrayList<Object>() {
                            {
                                add(new LinkedHashMap<String, Object>() {
                                    {
                                        put("@value", "ABC");
                                    }
                                });
                            }
                        });
                    }
                });
            }
        };

        final List<String> expected = new ArrayList<String>() {
            {
                add("<http://localhost:8080/foo1> <http://foo.com/code> \"123\"^^<http://www.w3.org/2001/XMLSchema#string> .");
                add("<http://localhost:8080/foo2> <http://foo.com/code> \"ABC\"^^<http://www.w3.org/2001/XMLSchema#string> .");
            }
        };

        final JSONLDTripleCallback callback = new JenaTripleCallback();
        final Model model = (Model) JSONLD.toRDF(input, callback);

        final StringWriter w = new StringWriter();
        model.write(w, "N-TRIPLE");

        final List<String> result = new ArrayList<String>(Arrays.asList(w.getBuffer().toString()
                .split(System.getProperty("line.separator"))));
        Collections.sort(result);

        assertTrue(JSONUtils.equals(expected, result));
    }

}

@@ -67,20 +67,22 @@
 t.assert
-True
+Equals
 ;%0A%0Aimpor
@@ -175,27 +175,23 @@
 va.util.
-Collections
+HashSet
 ;%0Aimport
@@ -260,16 +260,38 @@
 til.Map;
+%0Aimport java.util.Set;
 %0A%0Aimport
@@ -581,54 +581,8 @@
 ck;%0A
-import com.github.jsonldjava.utils.JSONUtils;%0A
 impo
@@ -2078,32 +2078,638 @@
             %7D);%0A
+                add(new LinkedHashMap%3CString, Object%3E() %7B%0A                    %7B%0A                        // Anonymous%0A                        put(%22http://foo.com/code%22, new ArrayList%3CObject%3E() %7B%0A                            %7B%0A                                add(new LinkedHashMap%3CString, Object%3E() %7B%0A                                    %7B%0A                                        put(%22@id%22, %22http://www.example.com/%22);%0A                                    %7D%0A                                %7D);%0A                            %7D%0A                        %7D);%0A                    %7D%0A                %7D);%0A                %0A
             %7D%0A  
@@ -2724,35 +2724,34 @@
 %0A%0A        final 
-Lis
+Se
 t%3CString%3E expect
@@ -2755,32 +2755,30 @@
 ected = new 
-ArrayLis
+HashSe
 t%3CString%3E() 
@@ -3041,32 +3041,111 @@
 ma#string%3E .%22);%0A
+                add(%22_:b0 %3Chttp://foo.com/code%3E %3Chttp://www.example.com/%3E .%22);%0A
             %7D%0A  
@@ -3378,24 +3378,64 @@
 -TRIPLE%22);%0A%0A
+        System.out.println(w);%0A        %0A
         fina
@@ -3436,19 +3436,18 @@
   final 
-Lis
+Se
 t%3CString
@@ -3461,24 +3461,22 @@
 t = new 
-ArrayLis
+HashSe
 t%3CString
@@ -3584,73 +3584,24 @@
 ));%0A
-        Collections.sort(result);%0A%0A        assertTrue(JSONUtils.e
+%0A        assertE
 qual
@@ -3619,18 +3619,25 @@
  result)
-)
 ;
+        
 %0A    %7D%0A%0A
@@ -3638,8 +3638,9 @@
    %7D%0A%0A%7D%0A
+%0A

Test for anonymous bnodes in Jena