/**
 * 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.commons.rdf.jena;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.apache.commons.rdf.api.BlankNode;
import org.apache.jena.sparql.core.Quad;
import org.junit.Test;

public class GeneralizedRDFQuadTest {

    private JenaRDF jena = new JenaRDF();
    
    @Test
    public void bnodeProperty() throws Exception {
        BlankNode b1 = jena.createBlankNode("b1");
        JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
        JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
        JenaIRI ex3 = jena.createIRI("http://example.com/ex3");
        
        JenaGeneralizedQuadLike q = jena.createGeneralizedQuad(ex1, b1, ex2, ex3);
        assertEquals(ex1, q.getSubject());
        assertEquals(ex2, q.getObject());
        assertEquals(b1, q.getPredicate()); // it's a bnode!
        assertEquals(ex3, q.getGraphName().get());
        assertTrue(q.asJenaQuad().getPredicate().isBlank());
    }

    @Test
    public void literalPredicate() throws Exception {
        JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
        JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
        JenaIRI ex3 = jena.createIRI("http://example.com/ex3");
        JenaLiteral lit = jena.createLiteral("Hello");
        
        JenaGeneralizedQuadLike q = jena.createGeneralizedQuad(ex1, lit, ex2, ex3);
        assertEquals(ex1, q.getSubject());
        assertEquals(ex2, q.getObject());
        assertEquals(lit, q.getPredicate()); // it's a literal!
        assertEquals(ex3, q.getGraphName().get());
        assertTrue(q.asJenaQuad().getPredicate().isLiteral());
    }


    @Test
    public void literalSubject() throws Exception {
        JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
        JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
        JenaIRI ex3 = jena.createIRI("http://example.com/ex3");
        JenaLiteral lit = jena.createLiteral("Hello");
        
        JenaGeneralizedQuadLike q = jena.createGeneralizedQuad(lit, ex1, ex2, ex3);
        assertEquals(lit, q.getSubject()); // it's a literal!
        assertEquals(ex1, q.getPredicate());
        assertEquals(ex2, q.getObject());
        assertEquals(ex3, q.getGraphName().get());
        assertTrue(q.asJenaQuad().getSubject().isLiteral());
    }
    
    @Test
    public void literalSubjectDefaultGraphGen() throws Exception {
        JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
        JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
        // No need to cast to JenaIRI
        JenaRDFTerm defG = jena.asRDFTerm(Quad.defaultGraphNodeGenerated); 
        JenaLiteral lit = jena.createLiteral("Hello");
        
        JenaGeneralizedQuadLike q = jena.createGeneralizedQuad(lit, ex1, ex2, defG);
        assertEquals(lit, q.getSubject()); // it's a literal!
        assertEquals(ex1, q.getPredicate());
        assertEquals(ex2, q.getObject());
        assertTrue(q.asJenaQuad().getSubject().isLiteral());
        assertFalse(q.getGraphName().isPresent());
        assertTrue(q.asJenaQuad().isDefaultGraph());
    }

    
    @Test
    public void literalGraph() throws Exception {
        JenaIRI ex1 = jena.createIRI("http://example.com/ex1");
        JenaIRI ex2 = jena.createIRI("http://example.com/ex2");
        JenaIRI ex3 = jena.createIRI("http://example.com/ex3");
        JenaLiteral lit = jena.createLiteral("Hello");
        
        JenaGeneralizedQuadLike q = jena.createGeneralizedQuad(ex1, ex2, ex3, lit);
        assertEquals(ex1, q.getSubject()); 
        assertEquals(ex2, q.getPredicate());
        assertEquals(ex3, q.getObject());
        assertEquals(lit, q.getGraphName().get()); // it's a literal!
        assertTrue(q.asJenaQuad().getGraph().isLiteral());
    }
    
    
}

@@ -873,110 +873,135 @@
 ert.
-assertEquals;%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertTru
+*;%0A%0Aimport org.apache.commons.rdf.api.BlankNode;%0Aimport org.apache.commons.rdf.api.RDFTerm;%0Aimport org.apache.jena.graph.Nod
 e;%0A
-%0A
 impo
@@ -1018,33 +1018,30 @@
 che.
-commons.rdf.api.BlankNode
+jena.graph.NodeFactory
 ;%0Aim
@@ -3984,24 +3984,660 @@
 ());%0A    %7D%0A%0A
+    @Test%0A    public void asGeneralizedQuad() throws Exception %7B%0A        Node s = NodeFactory.createLiteral(%22Hello%22);%0A        Node p = NodeFactory.createBlankNode();%0A        Node o = NodeFactory.createURI(%22http://example.com/ex%22);%0A        Node g = Quad.defaultGraphIRI;%0A        Quad jq = Quad.create(g, s, p, o);%0A        JenaQuadLike%3CRDFTerm%3E q = jena.asGeneralizedQuad(jq);%0A        assertEquals(jena.createLiteral(%22Hello%22), q.getSubject());%0A        assertEquals(jena.asRDFTerm(p), q.getPredicate());%0A        assertEquals(jena.createIRI(%22http://example.com/ex%22), q.getObject());%0A        assertFalse(q.getGraphName().isPresent());%0A    %7D%0A
     %0A    @Te
@@ -5305,10 +5305,15 @@
   %0A    %0A
+    %0A
 %7D%0A

Test asGeneralizedQuad() with default graph