This project has retired. For details please refer to its Attic page.
ExtractorsFlowTest 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  
18  package org.apache.any23.cli;
19  
20  import org.apache.any23.cli.flows.PeopleExtractor;
21  import org.apache.any23.rdf.RDFUtils;
22  import org.apache.commons.io.FileUtils;
23  import org.eclipse.rdf4j.model.Model;
24  import org.eclipse.rdf4j.model.impl.TreeModel;
25  import org.eclipse.rdf4j.rio.Rio;
26  import org.junit.Assert;
27  import org.junit.Test;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.io.BufferedInputStream;
32  import java.io.File;
33  import java.io.FileInputStream;
34  import java.lang.invoke.MethodHandles;
35  import java.util.Arrays;
36  import java.util.Locale;
37  import java.util.stream.Stream;
38  
39  /**
40   * This is example for task ANY23-396
41   *
42   * @author Jacek Grzebyta (jgrzebyta@apache.org)
43   * @author Hans Brende (hansbrende@apache.org)
44   */
45  public class ExtractorsFlowTest extends ToolTestBase {
46  
47      private static final String testingDatafile = "/org/apache/any23/extractor/csv/test-comma.csv";
48      private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49  
50      public ExtractorsFlowTest() {
51          super(Rover.class);
52      }
53  
54      /**
55       * Emulates action described in ANY23-396.
56       * 
57       * @throws Exception
58       *             if there is an error asserting the test data.
59       */
60      @Test
61      public void runTestFor396() throws Exception {
62          File outputFile = File.createTempFile("mockdata-", ".ttl", tempDirectory);
63          File logFile = File.createTempFile("log-exec-", ".txt", tempDirectory);
64  
65          runTool(String.format(Locale.ROOT, "-l %s -o %s -f people,turtle -e csv -d %s %s", logFile.getAbsolutePath(),
66                  outputFile.getAbsolutePath(), PeopleExtractor.RAW_NS,
67                  copyResourceToTempFile(testingDatafile).getAbsolutePath()));
68  
69          // populate expected model
70          Model expected = new TreeModel();
71          Stream.of("Davide Palmisano", "Michele Mostarda", "Giovanni Tummarello").map(PeopleExtractor::createPerson)
72                  .forEach(expected::addAll);
73  
74          if (log.isDebugEnabled()) {
75              log.debug("\n\nlog file content:\n{}", FileUtils.readFileToString(logFile, "utf-8"));
76              log.debug("\n\nData file: \n{}", FileUtils.readFileToString(outputFile, "utf-8"));
77          }
78  
79          Assert.assertTrue(assertCompareModels(expected, outputFile));
80      }
81  
82      /**
83       * Compare expected model and received from input File.
84       * 
85       * @throws Exception
86       *             if there is an error asserting the test data.
87       */
88      private boolean assertCompareModels(Model expected, File received) throws Exception {
89          Model receivedModel = new TreeModel();
90          receivedModel.addAll(Arrays.asList(
91                  RDFUtils.parseRDF(Rio.getParserFormatForFileName(received.getName()).orElseThrow(AssertionError::new),
92                          new BufferedInputStream(new FileInputStream(received)), received.toURI().toString())));
93  
94          return receivedModel.containsAll(expected);
95      }
96  }