This project has retired. For details please refer to its Attic page.
ElementsProcessorTest 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.io.StringWriter;
20  import java.nio.charset.StandardCharsets;
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import org.eclipse.rdf4j.model.Literal;
26  import org.eclipse.rdf4j.model.Model;
27  import org.eclipse.rdf4j.model.Resource;
28  import org.eclipse.rdf4j.model.vocabulary.RDF;
29  import org.eclipse.rdf4j.rio.RDFFormat;
30  import org.eclipse.rdf4j.rio.Rio;
31  import org.junit.Assert;
32  import org.junit.Test;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  /**
37   *
38   * @author Jacek Grzebyta (jgrzebyta [at] apache [dot] org)
39   */
40  public class ElementsProcessorTest {
41  
42      private final Logger log = LoggerFactory.getLogger(getClass());
43      private static final ElementsProcessor ep = ElementsProcessor.getInstance();
44  
45      @Test
46      public void processMap() throws Exception {
47          Map<String, Object> simpleMap = new HashMap<String, Object>() {
48              {
49                  put("key1", "value1");
50                  put("key2", "value2");
51                  put("key3", 3);
52              }
53          };
54  
55          ElementsProcessor.ModelHolder toTest = ep.processMap(ep.vf.createIRI("http://example.org/"), simpleMap,
56                  ep.vf.createIRI("http://example.org/node1"));
57  
58          Assert.assertEquals("http://example.org/node1", toTest.getRoot().stringValue()); // if parent node is not blank
59                                                                                           // than returns it as key
60          Assert.assertTrue(toTest.getModel().size() > 0);
61          log.debug("Model: \n{}\n", dumpModel(toTest.getModel(), RDFFormat.TURTLE));
62      }
63  
64      @Test
65      public void processList() throws Exception {
66          List<Object> simpleList = new ArrayList<Object>() {
67              {
68                  add("Ala");
69                  add(6);
70                  add("ma");
71                  add("k".getBytes(StandardCharsets.UTF_8)[0]);
72              }
73          };
74  
75          ElementsProcessor.ModelHolder toTest = ep.processList(ep.vf.createIRI("http://example.org/data"), simpleList);
76          Assert.assertNotNull(toTest);
77          Assert.assertTrue(toTest.getModel().contains(null, RDF.FIRST, ep.vf.createLiteral("Ala"), (Resource[]) null));
78          Assert.assertTrue(toTest.getModel().contains(null, RDF.FIRST, ep.vf.createLiteral(6), (Resource[]) null));
79          Assert.assertTrue(toTest.getModel().contains(null, RDF.FIRST, ep.vf.createLiteral("ma"), (Resource[]) null));
80          Assert.assertTrue(toTest.getModel().contains(null, RDF.FIRST,
81                  ep.vf.createLiteral("k".getBytes(StandardCharsets.UTF_8)[0]), (Resource[]) null));
82          log.debug("Model: \n{}\n", dumpModel(toTest.getModel(), RDFFormat.TURTLE));
83      }
84  
85      @Test
86      public void processSimple() throws Exception {
87          List<Object> simpleList = new ArrayList<Object>() {
88              {
89                  add("Ala");
90                  add(6);
91                  add("ma");
92                  add("k".getBytes(StandardCharsets.UTF_8)[0]);
93              }
94          };
95  
96          simpleList.forEach((i) -> {
97              ElementsProcessor.ModelHolder out = ep.asModel(ep.vf.createIRI("urn:test/"), i, null);
98              Assert.assertTrue(out.getRoot() instanceof Literal);
99              Assert.assertTrue(out.getModel().isEmpty());
100         });
101     }
102 
103     private String dumpModel(Model m, RDFFormat format) {
104         StringWriter writer = new StringWriter();
105         Rio.write(m, writer, format);
106         return writer.toString();
107     }
108 }