|
| 1 | +package com.baeldung.jena; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | + |
| 5 | +import java.io.ByteArrayOutputStream; |
| 6 | +import java.io.StringReader; |
| 7 | + |
| 8 | +import org.apache.jena.rdf.model.Model; |
| 9 | +import org.apache.jena.rdf.model.ModelFactory; |
| 10 | +import org.apache.jena.rdf.model.Resource; |
| 11 | +import org.apache.jena.riot.Lang; |
| 12 | +import org.apache.jena.riot.RDFDataMgr; |
| 13 | +import org.apache.jena.vocabulary.SchemaDO; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +public class RDFXMLUnitTest { |
| 17 | + @Test |
| 18 | + void whenWeHaveAModel_ThenWeCanSerializeAsRDFXMLUsingRDFDataMgr() { |
| 19 | + Model model = ModelFactory.createDefaultModel(); |
| 20 | + |
| 21 | + model.createResource("tag:baeldung:/blog/posts/123") |
| 22 | + .addProperty(SchemaDO.headline, "Introduction to RDF and Apache Jena") |
| 23 | + .addProperty(SchemaDO.wordCount, "835") |
| 24 | + .addProperty(SchemaDO.author, model.createResource("tag:baeldung:/users/Baeldung") |
| 25 | + .addProperty(SchemaDO.name, "Baeldung") |
| 26 | + .addProperty(SchemaDO.url, "https://baeldung.com")) |
| 27 | + .addProperty(SchemaDO.comment, model.createResource("tag:baeldung:/blog/posts/123/comments/1") |
| 28 | + .addProperty(SchemaDO.text, "What a great article!") |
| 29 | + .addProperty(SchemaDO.author, model.createResource("tag:baeldung:/users/JoeBloggs") |
| 30 | + .addProperty(SchemaDO.name, "Joe Bloggs") |
| 31 | + . addProperty( SchemaDO. email, "[email protected]"))); |
| 32 | + |
| 33 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 34 | + RDFDataMgr.write(out, model, Lang.RDFXML); |
| 35 | + |
| 36 | + assertEquals(""" |
| 37 | + <rdf:RDF |
| 38 | + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| 39 | + xmlns:j.0="https://schema.org/"> |
| 40 | + <rdf:Description rdf:about="tag:baeldung:/blog/posts/123"> |
| 41 | + <j.0:comment> |
| 42 | + <rdf:Description rdf:about="tag:baeldung:/blog/posts/123/comments/1"> |
| 43 | + <j.0:author> |
| 44 | + <rdf:Description rdf:about="tag:baeldung:/users/JoeBloggs"> |
| 45 | + <j.0:email>[email protected]</j.0:email> |
| 46 | + <j.0:name>Joe Bloggs</j.0:name> |
| 47 | + </rdf:Description> |
| 48 | + </j.0:author> |
| 49 | + <j.0:text>What a great article!</j.0:text> |
| 50 | + </rdf:Description> |
| 51 | + </j.0:comment> |
| 52 | + <j.0:author> |
| 53 | + <rdf:Description rdf:about="tag:baeldung:/users/Baeldung"> |
| 54 | + <j.0:url>https://baeldung.com</j.0:url> |
| 55 | + <j.0:name>Baeldung</j.0:name> |
| 56 | + </rdf:Description> |
| 57 | + </j.0:author> |
| 58 | + <j.0:wordCount>835</j.0:wordCount> |
| 59 | + <j.0:headline>Introduction to RDF and Apache Jena</j.0:headline> |
| 60 | + </rdf:Description> |
| 61 | + </rdf:RDF> |
| 62 | + """, out.toString()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void whenWeHaveAModel_ThenWeCanSerializeAsRDFXMLUsingWrite() { |
| 67 | + Model model = ModelFactory.createDefaultModel(); |
| 68 | + |
| 69 | + model.createResource("tag:baeldung:/blog/posts/123") |
| 70 | + .addProperty(SchemaDO.headline, "Introduction to RDF and Apache Jena") |
| 71 | + .addProperty(SchemaDO.wordCount, "835") |
| 72 | + .addProperty(SchemaDO.author, model.createResource("tag:baeldung:/users/Baeldung") |
| 73 | + .addProperty(SchemaDO.name, "Baeldung") |
| 74 | + .addProperty(SchemaDO.url, "https://baeldung.com")) |
| 75 | + .addProperty(SchemaDO.comment, model.createResource("tag:baeldung:/blog/posts/123/comments/1") |
| 76 | + .addProperty(SchemaDO.text, "What a great article!") |
| 77 | + .addProperty(SchemaDO.author, model.createResource("tag:baeldung:/users/JoeBloggs") |
| 78 | + .addProperty(SchemaDO.name, "Joe Bloggs") |
| 79 | + . addProperty( SchemaDO. email, "[email protected]"))); |
| 80 | + |
| 81 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 82 | + model.write(out); |
| 83 | + |
| 84 | + assertEquals(""" |
| 85 | + <rdf:RDF |
| 86 | + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| 87 | + xmlns:j.0="https://schema.org/"> |
| 88 | + <rdf:Description rdf:about="tag:baeldung:/blog/posts/123"> |
| 89 | + <j.0:comment> |
| 90 | + <rdf:Description rdf:about="tag:baeldung:/blog/posts/123/comments/1"> |
| 91 | + <j.0:author> |
| 92 | + <rdf:Description rdf:about="tag:baeldung:/users/JoeBloggs"> |
| 93 | + <j.0:email>[email protected]</j.0:email> |
| 94 | + <j.0:name>Joe Bloggs</j.0:name> |
| 95 | + </rdf:Description> |
| 96 | + </j.0:author> |
| 97 | + <j.0:text>What a great article!</j.0:text> |
| 98 | + </rdf:Description> |
| 99 | + </j.0:comment> |
| 100 | + <j.0:author> |
| 101 | + <rdf:Description rdf:about="tag:baeldung:/users/Baeldung"> |
| 102 | + <j.0:url>https://baeldung.com</j.0:url> |
| 103 | + <j.0:name>Baeldung</j.0:name> |
| 104 | + </rdf:Description> |
| 105 | + </j.0:author> |
| 106 | + <j.0:wordCount>835</j.0:wordCount> |
| 107 | + <j.0:headline>Introduction to RDF and Apache Jena</j.0:headline> |
| 108 | + </rdf:Description> |
| 109 | + </rdf:RDF> |
| 110 | + """, out.toString()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void whenWeHaveRDFXML_thenWeCanParseThis() { |
| 115 | + String rdfxml = """ |
| 116 | + <rdf:RDF |
| 117 | + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" |
| 118 | + xmlns:j.0="https://schema.org/"> |
| 119 | + <rdf:Description rdf:about="tag:baeldung:/blog/posts/123"> |
| 120 | + <j.0:comment> |
| 121 | + <rdf:Description rdf:about="tag:baeldung:/blog/posts/123/comments/1"> |
| 122 | + <j.0:author> |
| 123 | + <rdf:Description rdf:about="tag:baeldung:/users/JoeBloggs"> |
| 124 | + <j.0:email>[email protected]</j.0:email> |
| 125 | + <j.0:name>Joe Bloggs</j.0:name> |
| 126 | + </rdf:Description> |
| 127 | + </j.0:author> |
| 128 | + <j.0:text>What a great article!</j.0:text> |
| 129 | + </rdf:Description> |
| 130 | + </j.0:comment> |
| 131 | + <j.0:author> |
| 132 | + <rdf:Description rdf:about="tag:baeldung:/users/Baeldung"> |
| 133 | + <j.0:url>https://baeldung.com</j.0:url> |
| 134 | + <j.0:name>Baeldung</j.0:name> |
| 135 | + </rdf:Description> |
| 136 | + </j.0:author> |
| 137 | + <j.0:wordCount>835</j.0:wordCount> |
| 138 | + <j.0:headline>Introduction to RDF and Apache Jena</j.0:headline> |
| 139 | + </rdf:Description> |
| 140 | + </rdf:RDF> |
| 141 | + """; |
| 142 | + |
| 143 | + Model model = ModelFactory.createDefaultModel(); |
| 144 | + model.read(new StringReader(rdfxml), null); |
| 145 | + |
| 146 | + Resource blogPost = model.getResource("tag:baeldung:/blog/posts/123"); |
| 147 | + |
| 148 | + assertEquals("Introduction to RDF and Apache Jena", blogPost.getProperty(SchemaDO.headline).getString()); |
| 149 | + |
| 150 | + Resource author = blogPost.getProperty(SchemaDO.author).getResource(); |
| 151 | + assertEquals("Baeldung", author.getProperty(SchemaDO.name).getString()); |
| 152 | + |
| 153 | + } |
| 154 | +} |
0 commit comments