|
18 | 18 | package cz.cvut.kbss.jsonld.serialization;
|
19 | 19 |
|
20 | 20 | import cz.cvut.kbss.jopa.model.annotations.Id;
|
21 |
| -import cz.cvut.kbss.jopa.model.annotations.OWLAnnotationProperty; |
22 | 21 | import cz.cvut.kbss.jopa.model.annotations.OWLClass;
|
23 | 22 | import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
|
24 |
| -import cz.cvut.kbss.jopa.vocabulary.DC; |
25 | 23 | import cz.cvut.kbss.jopa.vocabulary.XSD;
|
26 | 24 | import cz.cvut.kbss.jsonld.ConfigParam;
|
27 | 25 | import cz.cvut.kbss.jsonld.JsonLd;
|
|
34 | 32 | import cz.cvut.kbss.jsonld.environment.model.GeneratesRdf;
|
35 | 33 | import cz.cvut.kbss.jsonld.environment.model.GenericMember;
|
36 | 34 | import cz.cvut.kbss.jsonld.environment.model.ObjectWithAnnotationProperties;
|
| 35 | +import cz.cvut.kbss.jsonld.environment.model.ObjectWithNumericAttributes; |
37 | 36 | import cz.cvut.kbss.jsonld.environment.model.Organization;
|
38 | 37 | import cz.cvut.kbss.jsonld.environment.model.OwlPropertyType;
|
39 | 38 | import cz.cvut.kbss.jsonld.environment.model.Person;
|
|
50 | 49 | import jakarta.json.JsonObject;
|
51 | 50 | import jakarta.json.JsonValue;
|
52 | 51 | import org.eclipse.rdf4j.model.BNode;
|
53 |
| -import org.eclipse.rdf4j.model.IRI; |
54 | 52 | import org.eclipse.rdf4j.model.Model;
|
55 | 53 | import org.eclipse.rdf4j.model.Statement;
|
56 | 54 | import org.eclipse.rdf4j.model.ValueFactory;
|
|
65 | 63 | import java.io.ByteArrayInputStream;
|
66 | 64 | import java.io.IOException;
|
67 | 65 | import java.io.StringReader;
|
| 66 | +import java.math.BigDecimal; |
| 67 | +import java.math.BigInteger; |
68 | 68 | import java.net.URI;
|
69 | 69 | import java.nio.charset.StandardCharsets;
|
70 | 70 | import java.time.LocalDate;
|
|
83 | 83 | import static org.hamcrest.MatcherAssert.assertThat;
|
84 | 84 | import static org.hamcrest.Matchers.hasItem;
|
85 | 85 | import static org.hamcrest.Matchers.hasItems;
|
| 86 | +import static org.hamcrest.Matchers.in; |
86 | 87 | import static org.hamcrest.Matchers.startsWith;
|
87 | 88 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
88 | 89 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
@@ -483,41 +484,30 @@ void serializationSerializesAttributeWithCollectionOfEnumConstantsMappedToIndivi
|
483 | 484 |
|
484 | 485 | @Test
|
485 | 486 | void serializationIncludesDatatypeOfNumericLiterals() throws Exception {
|
486 |
| - final Product p = new Product(); |
487 |
| - p.price = 155.15; |
488 |
| - p.name = "Test product"; |
489 |
| - p.uri = Generator.generateUri(); |
490 |
| - sut.serialize(p); |
| 487 | + final ObjectWithNumericAttributes instance = new ObjectWithNumericAttributes(Generator.generateUri()); |
| 488 | + instance.setDoubleValue(155.15); |
| 489 | + instance.setFloatValue(155.15f); |
| 490 | + instance.setLongValue(155L); |
| 491 | + instance.setShortValue((short) 155); |
| 492 | + instance.setIntValue(155); |
| 493 | + instance.setBigIntegerValue(BigInteger.valueOf(155L)); |
| 494 | + instance.setBigDecimalValue(BigDecimal.valueOf(155.15)); |
| 495 | + sut.serialize(instance); |
491 | 496 | final JsonArray result = parseAndExpand(jsonWriter.getResult());
|
492 | 497 | final JsonObject obj = result.getJsonObject(0);
|
493 |
| - final JsonArray priceAtt = obj.getJsonArray("https://schema.org/price"); |
494 |
| - assertEquals(1, priceAtt.size()); |
495 |
| - assertEquals(XSD.DOUBLE, priceAtt.getJsonObject(0).getString("@type")); |
496 |
| - assertEquals(p.price.toString(), priceAtt.getJsonObject(0).getJsonNumber("@value").toString()); |
497 |
| - } |
498 |
| - |
499 |
| - @OWLClass(iri = Vocabulary.DEFAULT_PREFIX + "Product") |
500 |
| - static class Product implements GeneratesRdf { |
501 |
| - @Id |
502 |
| - URI uri; |
503 |
| - |
504 |
| - @OWLAnnotationProperty(iri = DC.Terms.TITLE) |
505 |
| - String name; |
506 |
| - |
507 |
| - @OWLDataProperty(iri = "https://schema.org/price") |
508 |
| - Double price; |
509 |
| - |
510 |
| - @Override |
511 |
| - public URI getUri() { |
512 |
| - return uri; |
513 |
| - } |
514 |
| - |
515 |
| - @Override |
516 |
| - public void toRdf(Model model, ValueFactory vf, Set<URI> visited) { |
517 |
| - final IRI subject = vf.createIRI(uri.toString()); |
518 |
| - model.add(subject, RDF.TYPE, vf.createIRI(Vocabulary.DEFAULT_PREFIX + "Product")); |
519 |
| - model.add(subject, vf.createIRI(DC.Terms.TITLE), vf.createLiteral(name)); |
520 |
| - model.add(subject, vf.createIRI("https://schema.org/price"), vf.createLiteral(price)); |
521 |
| - } |
| 498 | + checkValueDatatype(obj, Vocabulary.DEFAULT_PREFIX + "doubleValue", XSD.DOUBLE, instance.getDoubleValue()); |
| 499 | + checkValueDatatype(obj, Vocabulary.DEFAULT_PREFIX + "floatValue", XSD.FLOAT, instance.getFloatValue()); |
| 500 | + checkValueDatatype(obj, Vocabulary.DEFAULT_PREFIX + "longValue", XSD.LONG, instance.getLongValue()); |
| 501 | + checkValueDatatype(obj, Vocabulary.DEFAULT_PREFIX + "shortValue", XSD.SHORT, instance.getShortValue()); |
| 502 | + checkValueDatatype(obj, Vocabulary.DEFAULT_PREFIX + "intValue", XSD.INT, instance.getIntValue()); |
| 503 | + checkValueDatatype(obj, Vocabulary.DEFAULT_PREFIX + "bigIntegerValue", XSD.INTEGER, instance.getBigIntegerValue()); |
| 504 | + checkValueDatatype(obj, Vocabulary.DEFAULT_PREFIX + "bigDecimalValue", XSD.DECIMAL, instance.getBigDecimalValue()); |
| 505 | + } |
| 506 | + |
| 507 | + private static void checkValueDatatype(JsonObject result, String attIri, String datatype, Number value) { |
| 508 | + final JsonArray att = result.getJsonArray(attIri); |
| 509 | + assertEquals(1, att.size()); |
| 510 | + assertEquals(datatype, att.getJsonObject(0).getString("@type")); |
| 511 | + assertEquals(value.toString(), att.getJsonObject(0).getJsonNumber("@value").toString()); |
522 | 512 | }
|
523 | 513 | }
|
0 commit comments