This project has retired. For details please refer to its Attic page.
JSONWriterTest xref
View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *  http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.any23.writer;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.nio.charset.StandardCharsets;
22  
23  import org.apache.any23.extractor.ExtractionContext;
24  import org.eclipse.rdf4j.model.IRI;
25  import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  /**
30   * Test case for {@link JSONLDWriter} and deprecated JSONWriter classes.
31   *
32   * @author Michele Mostarda (mostarda@fbk.eu)
33   * @author Julio Caguano
34   */
35  public class JSONWriterTest {
36  
37      @Test
38      @Deprecated
39      public void testJSONWriting() throws TripleHandlerException, IOException {
40          ByteArrayOutputStream baos = new ByteArrayOutputStream();
41          writeContentComplicated(new JSONWriter(baos));
42  
43          final String expected = "{\n" + "  \"quads\" : [ [ {\n" + "    \"type\" : \"bnode\",\n"
44                  + "    \"value\" : \"bn1\"\n" + "  }, \"http://pred/1\", {\n" + "    \"type\" : \"uri\",\n"
45                  + "    \"value\" : \"http://value/1\"\n" + "  }, \"http://graph/1\" ], [ {\n"
46                  + "    \"type\" : \"uri\",\n" + "    \"value\" : \"http://sub/2\"\n" + "  }, \"http://pred/2\", {\n"
47                  + "    \"type\" : \"literal\",\n" + "    \"value\" : \"language literal\",\n"
48                  + "    \"lang\" : \"en\",\n"
49                  + "    \"datatype\" : \"http://www.w3.org/1999/02/22-rdf-syntax-ns#langString\"\n"
50                  + "  }, \"http://graph/2\" ], [ {\n" + "    \"type\" : \"uri\",\n"
51                  + "    \"value\" : \"http://sub/3\"\n" + "  }, \"http://pred/3\", {\n" + "    \"type\" : \"literal\",\n"
52                  + "    \"value\" : \"123\",\n" + "    \"lang\" : null,\n" + "    \"datatype\" : \"http://datatype\"\n"
53                  + "  }, null ] ]\n" + "}";
54          Assert.assertEquals(expected, baos.toString(StandardCharsets.UTF_8));
55  
56          baos.reset();
57          writeContentSimple(new JSONWriter(baos));
58          Assert.assertEquals(expected, baos.toString(StandardCharsets.UTF_8));
59      }
60  
61      @Test
62      public void testJSONLDWriting() throws TripleHandlerException {
63          ByteArrayOutputStream baos = new ByteArrayOutputStream();
64          writeContentComplicated(new JSONLDWriter(baos));
65          final String expected = "[ {\n" + "  \"@graph\" : [ {\n" + "    \"@id\" : \"http://sub/3\",\n"
66                  + "    \"http://pred/3\" : [ {\n" + "      \"@type\" : \"http://datatype\",\n"
67                  + "      \"@value\" : \"123\"\n" + "    } ]\n" + "  } ],\n" + "  \"@id\" : \"http://any23.org/tmp/\"\n"
68                  + "}, {\n" + "  \"@graph\" : [ {\n" + "    \"@id\" : \"_:bn1\",\n" + "    \"http://pred/1\" : [ {\n"
69                  + "      \"@id\" : \"http://value/1\"\n" + "    } ]\n" + "  } ],\n" + "  \"@id\" : \"http://graph/1\"\n"
70                  + "}, {\n" + "  \"@graph\" : [ {\n" + "    \"@id\" : \"http://sub/2\",\n"
71                  + "    \"http://pred/2\" : [ {\n" + "      \"@language\" : \"en\",\n"
72                  + "      \"@value\" : \"language literal\"\n" + "    } ]\n" + "  } ],\n"
73                  + "  \"@id\" : \"http://graph/2\"\n" + "} ]";
74          Assert.assertEquals(expected, baos.toString(StandardCharsets.UTF_8));
75  
76          baos.reset();
77          writeContentSimple(new JSONLDWriter(baos));
78          Assert.assertEquals(expected, baos.toString(StandardCharsets.UTF_8));
79      }
80  
81      private void writeContentSimple(TripleWriter writer) throws TripleHandlerException {
82          writer.writeTriple(SimpleValueFactory.getInstance().createBNode("bn1"),
83                  SimpleValueFactory.getInstance().createIRI("http://pred/1"),
84                  SimpleValueFactory.getInstance().createIRI("http://value/1"),
85                  SimpleValueFactory.getInstance().createIRI("http://graph/1"));
86  
87          writer.writeTriple(SimpleValueFactory.getInstance().createIRI("http://sub/2"),
88                  SimpleValueFactory.getInstance().createIRI("http://pred/2"),
89                  SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
90                  SimpleValueFactory.getInstance().createIRI("http://graph/2"));
91  
92          writer.writeTriple(SimpleValueFactory.getInstance().createIRI("http://sub/3"),
93                  SimpleValueFactory.getInstance().createIRI("http://pred/3"),
94                  SimpleValueFactory.getInstance().createLiteral("123",
95                          SimpleValueFactory.getInstance().createIRI("http://datatype")),
96                  writer instanceof JSONLDWriter ? SimpleValueFactory.getInstance().createIRI("http://any23.org/tmp/")
97                          : null);
98  
99          writer.close();
100 
101     }
102 
103     private void writeContentComplicated(TripleHandler writer) throws TripleHandlerException {
104         // creating a fake document uri in order to write triples is terrible.
105         // see improved solution in "writeContentSimple"!
106         final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
107         writer.startDocument(documentIRI);
108         writer.receiveTriple(SimpleValueFactory.getInstance().createBNode("bn1"),
109                 SimpleValueFactory.getInstance().createIRI("http://pred/1"),
110                 SimpleValueFactory.getInstance().createIRI("http://value/1"),
111                 SimpleValueFactory.getInstance().createIRI("http://graph/1"), null);
112         writer.receiveTriple(SimpleValueFactory.getInstance().createIRI("http://sub/2"),
113                 SimpleValueFactory.getInstance().createIRI("http://pred/2"),
114                 SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
115                 SimpleValueFactory.getInstance().createIRI("http://graph/2"), null);
116         if (!(writer instanceof JSONLDWriter)) {
117             writer.receiveTriple(SimpleValueFactory.getInstance().createIRI("http://sub/3"),
118                     SimpleValueFactory.getInstance().createIRI("http://pred/3"), SimpleValueFactory.getInstance()
119                             .createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
120                     null, null);
121         } else {
122             // creating a fake extraction context in order to write triples is terrible.
123             // see improved solution in "writeContentSimple"!
124             ExtractionContext extractionContext = new ExtractionContext("rdf-nq",
125                     SimpleValueFactory.getInstance().createIRI("http://any23.org/tmp/"));
126             writer.receiveTriple(SimpleValueFactory.getInstance().createIRI("http://sub/3"),
127                     SimpleValueFactory.getInstance().createIRI("http://pred/3"), SimpleValueFactory.getInstance()
128                             .createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
129                     null, extractionContext);
130         }
131         writer.endDocument(documentIRI);
132         writer.close();
133     }
134 }