Debian Codegraph @

wdtk-rdf/src/test/java/org/wikidata/wdtk/rdf/values/ValueRdfConverterTest.java

raw
1 package org.wikidata.wdtk.rdf.values;
2
3 /*
4 * #%L
5 * Wikidata Toolkit RDF
6 * %%
7 * Copyright (C) 2014 Wikidata Toolkit Developers
8 * %%
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * #L%
21 */
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.math.BigDecimal;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.eclipse.rdf4j.model.BNode;
33 import org.eclipse.rdf4j.model.Model;
34 import org.eclipse.rdf4j.model.Resource;
35 import org.eclipse.rdf4j.model.Value;
36 import org.eclipse.rdf4j.rio.RDFFormat;
37 import org.eclipse.rdf4j.rio.RDFHandlerException;
38 import org.eclipse.rdf4j.rio.RDFParseException;
39 import org.wikidata.wdtk.datamodel.helpers.DatamodelMapper;
40 import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl;
41 import org.wikidata.wdtk.datamodel.implementation.UnsupportedEntityIdValueImpl;
42 import org.wikidata.wdtk.datamodel.interfaces.DataObjectFactory;
43 import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
44 import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
45 import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
46 import org.wikidata.wdtk.datamodel.interfaces.QuantityValue;
47 import org.wikidata.wdtk.datamodel.interfaces.TimeValue;
48 import org.wikidata.wdtk.datamodel.interfaces.UnsupportedEntityIdValue;
49 import org.wikidata.wdtk.rdf.MockPropertyRegister;
50 import org.wikidata.wdtk.rdf.OwlDeclarationBuffer;
51 import org.wikidata.wdtk.rdf.PropertyRegister;
52 import org.wikidata.wdtk.rdf.RdfTestHelpers;
53 import org.wikidata.wdtk.rdf.RdfWriter;
54
55 public class ValueRdfConverterTest {
56
57 ByteArrayOutputStream out;
58 RdfWriter rdfWriter;
59 OwlDeclarationBuffer rdfConversionBuffer;
60 PropertyRegister propertyRegister = new MockPropertyRegister();
61 DatamodelMapper mapper = new DatamodelMapper("http://www.wikidata.org/entity/");
62
63 DataObjectFactory objectFactory = new DataObjectFactoryImpl();
64
65 @Before
66 public void setUp() {
67 this.out = new ByteArrayOutputStream();
68 this.rdfWriter = new RdfWriter(RDFFormat.TURTLE, this.out);
69 this.rdfConversionBuffer = new OwlDeclarationBuffer();
70 this.rdfWriter.start();
71 }
72
73 @Test
74 public void testWriteQuantityValue() throws RDFHandlerException,
75 RDFParseException, IOException {
76 QuantityValueConverter valueConverter = new QuantityValueConverter(
77 this.rdfWriter, this.propertyRegister, this.rdfConversionBuffer);
78
79 QuantityValue value = this.objectFactory.getQuantityValue(
80 new BigDecimal(100), new BigDecimal(100), new BigDecimal(100));
81 PropertyIdValue propertyIdValue = objectFactory.getPropertyIdValue(
82 "P1081", "http://www.wikidata.org/entity/");
83 Value valueURI = valueConverter.getRdfValue(value, propertyIdValue,
84 false);
85 valueConverter.writeValue(value, (Resource) valueURI);
86 this.rdfWriter.finish();
87 Model model = RdfTestHelpers.parseRdf(this.out.toString());
88 assertEquals(model, RdfTestHelpers.parseRdf(RdfTestHelpers
89 .getResourceFromFile("QuantityValue.rdf")));
90 }
91
92 @Test
93 public void testWriteUnboundedQuantityValue() throws RDFHandlerException,
94 RDFParseException, IOException {
95 QuantityValueConverter valueConverter = new QuantityValueConverter(
96 this.rdfWriter, this.propertyRegister, this.rdfConversionBuffer);
97
98 QuantityValue value = this.objectFactory.getQuantityValue(new BigDecimal(100));
99 PropertyIdValue propertyIdValue = objectFactory.getPropertyIdValue(
100 "P1081", "http://www.wikidata.org/entity/");
101 Value valueURI = valueConverter.getRdfValue(value, propertyIdValue,
102 false);
103 valueConverter.writeValue(value, (Resource) valueURI);
104 this.rdfWriter.finish();
105 Model model = RdfTestHelpers.parseRdf(this.out.toString());
106 assertEquals(model, RdfTestHelpers.parseRdf(RdfTestHelpers
107 .getResourceFromFile("UnboundedQuantityValue.rdf")));
108 }
109
110 @Test
111 public void testWriteMonolingualTextValue() throws RDFHandlerException {
112 MonolingualTextValueConverter valueConverter = new MonolingualTextValueConverter(
113 this.rdfWriter, this.propertyRegister, this.rdfConversionBuffer);
114
115 MonolingualTextValue value = this.objectFactory
116 .getMonolingualTextValue("中华人民共和国", "zh-hans");
117 PropertyIdValue propertyIdValue = this.objectFactory
118 .getPropertyIdValue("P1448", "http://www.wikidata.org/entity/");
119 Value valueURI = valueConverter.getRdfValue(value, propertyIdValue,
120 true);
121 this.rdfWriter.finish();
122
123 assertEquals(valueURI.toString(), "\"中华人民共和国\"@zh-Hans");
124 }
125
126 @Test
127 public void testWriteGlobeCoordinatesValue() throws RDFHandlerException,
128 RDFParseException, IOException {
129 GlobeCoordinatesValueConverter valueConverter = new GlobeCoordinatesValueConverter(
130 this.rdfWriter, this.propertyRegister, this.rdfConversionBuffer);
131
132 GlobeCoordinatesValue value = this.objectFactory
133 .getGlobeCoordinatesValue(51.033333333333, 13.733333333333,
134 (GlobeCoordinatesValue.PREC_DECI_DEGREE),
135 "http://www.wikidata.org/entity/Q2");
136 PropertyIdValue propertyIdValue = objectFactory.getPropertyIdValue(
137 "P625", "http://www.wikidata.org/entity/");
138 Value valueURI = valueConverter.getRdfValue(value, propertyIdValue,
139 false);
140 valueConverter.writeValue(value, (Resource) valueURI);
141 this.rdfWriter.finish();
142 Model model = RdfTestHelpers.parseRdf(this.out.toString());
143 assertEquals(model, RdfTestHelpers.parseRdf(RdfTestHelpers
144 .getResourceFromFile("GlobeCoordinatesValue.rdf")));
145 }
146
147 @Test
148 public void testWriteTimeValue() throws RDFHandlerException,
149 RDFParseException, IOException {
150 TimeValueConverter valueConverter = new TimeValueConverter(
151 this.rdfWriter, this.propertyRegister, this.rdfConversionBuffer);
152
153 TimeValue value = objectFactory.getTimeValue(2008, (byte) 1, (byte) 1,
154 (byte) 0, (byte) 0, (byte) 0, (byte) 9, 0, 0, 0,
155 "http://www.wikidata.org/entity/Q1985727");
156 PropertyIdValue propertyIdValue = objectFactory.getPropertyIdValue(
157 "P569", "http://www.wikidata.org/entity/");
158 Value valueURI = valueConverter.getRdfValue(value, propertyIdValue,
159 false);
160 valueConverter.writeValue(value, (Resource) valueURI);
161 this.rdfWriter.finish();
162 Model model = RdfTestHelpers.parseRdf(this.out.toString());
163 assertEquals(model, RdfTestHelpers.parseRdf(RdfTestHelpers
164 .getResourceFromFile("TimeValue.rdf")));
165 }
166
167 @Test
168 public void testWriteUnsupportedEntityIdValue() throws RDFHandlerException,
169 RDFParseException, IOException {
170 AnyValueConverter valueConverter = new AnyValueConverter(
171 this.rdfWriter, this.rdfConversionBuffer, this.propertyRegister);
172
173 UnsupportedEntityIdValue value = mapper.readValue(
174 "{\"type\":\"wikibase-entityid\",\"value\":{\"entity-type\":\"funky\",\"id\":\"Z343\"}}",
175 UnsupportedEntityIdValueImpl.class);
176 PropertyIdValue propertyIdValue = objectFactory.getPropertyIdValue(
177 "P569", "http://www.wikidata.org/entity/");
178 Value valueURI = valueConverter.getRdfValue(value, propertyIdValue,
179 false);
180 this.rdfWriter.finish();
181 assertTrue(valueURI instanceof BNode);
182 }
183
184 }