This project has retired. For details please refer to its Attic page.
YAMLExtractorTest 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.extractor.yaml;
18  
19  import java.util.List;
20  import java.util.function.Function;
21  import java.util.stream.Collectors;
22  import org.apache.any23.extractor.ExtractorFactory;
23  import org.apache.any23.extractor.html.AbstractExtractorTestCase;
24  import org.apache.any23.rdf.RDFUtils;
25  import org.apache.any23.vocab.YAML;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.eclipse.rdf4j.common.iteration.Iterations;
29  import org.eclipse.rdf4j.model.Statement;
30  import org.eclipse.rdf4j.model.vocabulary.RDF;
31  import org.eclipse.rdf4j.model.vocabulary.RDFS;
32  import org.eclipse.rdf4j.query.BindingSet;
33  import org.eclipse.rdf4j.query.TupleQueryResult;
34  import org.eclipse.rdf4j.repository.RepositoryConnection;
35  import org.eclipse.rdf4j.repository.RepositoryResult;
36  import org.junit.ComparisonFailure;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Test {@link YAMLExtractor}.
42   *
43   * @author Jacek Grzebyta (jgrzebyta [at] apache [dot] org)
44   */
45  public class YAMLExtractorTest extends AbstractExtractorTestCase {
46  
47      public static final Logger log = LoggerFactory.getLogger(YAMLExtractorTest.class);
48      public static final String ns = "http://bob.example.com/";
49      private static final YAML vocab = YAML.getInstance();
50  
51      @Override
52      protected ExtractorFactory<?> getExtractorFactory() {
53          return new YAMLExtractorFactory();
54      }
55  
56      /**
57       * Test to validate simple yaml file for ANY23-312
58       * 
59       * @throws Exception
60       *             if there is an error asserting the test data.
61       */
62      @Test
63      public void simpleTest312() throws Exception {
64          assertExtract("/org/apache/any23/extractor/yaml/simple-312.yml");
65          log.debug(dumpModelToTurtle());
66          assertModelNotEmpty();
67      }
68  
69      @Test
70      public void simpleFileLoading() throws Exception {
71          assertExtract("/org/apache/any23/extractor/yaml/simple-load.yml");
72          log.debug("\n{}", dumpModelToTurtle());
73          assertModelNotEmpty();
74  
75      }
76  
77      @Test
78      public void integersTest() throws Exception {
79          assertExtract("/org/apache/any23/extractor/yaml/different-integers.yml");
80          log.debug(dumpModelToTurtle());
81          assertModelNotEmpty();
82          assertContains(null, RDFS.LABEL, RDFUtils.literal("hexadecimal"));
83          assertContains(null, RDFS.LABEL, RDFUtils.literal("octal"));
84      }
85  
86      @Test
87      public void floatsTest() throws Exception {
88          assertExtract("/org/apache/any23/extractor/yaml/different-float.yml");
89          log.debug(dumpModelToTurtle());
90          assertModelNotEmpty();
91      }
92  
93      @Test
94      public void multiTest() throws Exception {
95          assertExtract("/org/apache/any23/extractor/yaml/multi-test.yml");
96          log.debug(dumpModelToTurtle());
97          assertModelNotEmpty();
98          RepositoryResult<Statement> docs = getStatements(null, RDF.TYPE, vocab.document);
99          Assert.assertTrue(Iterations.asList(docs).size() > 1);
100     }
101 
102     @Test
103     public void nullTest() throws Exception {
104         assertExtract("/org/apache/any23/extractor/yaml/test-null.yml");
105         log.debug(dumpModelToTurtle());
106         assertModelNotEmpty();
107         int statements = dumpAsListOfStatements().size();
108         Assert.assertTrue("Found " + statements + " statements", statements == 26);
109     }
110 
111     @Test
112     public void treeTest() throws Exception {
113         assertExtract("/org/apache/any23/extractor/yaml/tree.yml");
114         log.debug("\n{}", dumpModelToTurtle());
115         assertModelNotEmpty();
116         // validate part of the tree structure
117         assertContainsModel(
118                 new Statement[] { RDFUtils.triple(RDFUtils.bnode(), RDFUtils.iri(ns, "value3"), RDFUtils.bnode("10")),
119                         RDFUtils.triple(RDFUtils.bnode("10"), RDF.FIRST, RDFUtils.bnode("11")),
120                         RDFUtils.triple(RDFUtils.bnode("11"), RDFUtils.iri(ns, "key3.1"), RDFUtils.bnode("12")),
121                         RDFUtils.triple(RDFUtils.bnode("12"), RDF.FIRST, RDFUtils.literal("value3.1.1")) });
122 
123         // validate occurence of <urn:value1> resource
124         assertContains(RDFUtils.triple(RDFUtils.bnode(), RDF.FIRST, RDFUtils.iri("urn:value1")));
125     }
126 
127     @Test
128     public void treeTest2() throws Exception {
129         assertExtract("/org/apache/any23/extractor/yaml/tree.yml");
130         String sparql = "select ?nodes where "
131                 + "{ [] <http://bob.example.com/key3.1> [ rdf:rest*/rdf:first ?nodes ;]}";
132 
133         RepositoryConnection connection = getConnection();
134         TupleQueryResult res = connection.prepareTupleQuery(sparql).evaluate();
135         List<BindingSet> resList = Iterations.asList(res);
136         try {
137             Assert.assertEquals("value3.1.1", resList.get(0).getValue("nodes").stringValue());
138         } catch (ComparisonFailure e) {
139             if ("value3.1.1".equals(resList.get(0).getValue("nodes").stringValue())) {
140                 throw new RuntimeException("there should be no error");
141             }
142         }
143         List<String> resString = resList.stream().map((Function<? super BindingSet, String>) (b) -> {
144             return b.getValue("nodes").stringValue();
145         }).collect(Collectors.toList());
146 
147         log.debug("List output: {}", resString);
148     }
149 
150     /**
151      * This test covers a typical situation when a document is a map.
152      * 
153      * <br>
154      * <b>NB:</b> Following yaml standard those 2 cases are parsed to different graphs: <br>
155      * Case 1:
156      * 
157      * <pre>
158      * ---
159      * key1: value1
160      * key2: value2
161      * key3: Some text  value, maybe description
162      * </pre>
163      * 
164      * Case 2:
165      * 
166      * <pre>
167      * ---
168      * - key1: value1
169      * - key2: value2
170      * - key3: Some text  value, maybe description
171      * </pre>
172      * 
173      * @throws Exception
174      *             if there is an error asserting the test data.
175      * 
176      * @see #treeTest()
177      */
178     @Test
179     public void tree2Test() throws Exception {
180         assertExtract("/org/apache/any23/extractor/yaml/tree2.yml");
181         log.debug("\n{}", dumpModelToTurtle());
182         assertModelNotEmpty();
183 
184         // check if document is type of mapping.
185         assertContainsModel(new Statement[] { RDFUtils.triple(RDFUtils.bnode("10"), RDF.TYPE, vocab.document),
186                 RDFUtils.triple(RDFUtils.bnode("10"), RDF.TYPE, vocab.mapping) });
187     }
188 }