1   package eu.fbk.knowledgestore.datastore;
2   
3   import java.io.IOException;
4   import java.util.ArrayList;
5   import java.util.HashSet;
6   import java.util.List;
7   
8   import org.junit.After;
9   import org.junit.Assert;
10  import org.junit.Before;
11  import org.junit.Test;
12  import org.openrdf.model.URI;
13  import org.openrdf.model.impl.URIImpl;
14  import org.openrdf.model.vocabulary.RDF;
15  
16  import eu.fbk.knowledgestore.data.Record;
17  import eu.fbk.knowledgestore.data.Stream;
18  import eu.fbk.knowledgestore.vocabulary.KS;
19  
20  /**
21   * Abstract class for defining data store tests.
22   */
23  public abstract class AbstractDataStoreTest {
24  
25      /** Data store to be used. */
26      private DataStore dataStore;
27  
28      protected abstract DataStore createDataStore();
29  
30      /**
31       * @return the dataStore
32       */
33      protected final DataStore getDataStore() {
34          return this.dataStore;
35      }
36  
37      @Before
38      public void setUp() throws IOException {
39          this.dataStore = createDataStore();
40          this.dataStore.init();
41      }
42  
43      @After
44      public void tearDown() throws IOException {
45          this.dataStore.close();
46      }
47  
48      @Test
49      public void testDataModifyResources() throws Throwable {
50          try {
51              final List<Record> records = createRecords(3, KS.RESOURCE);
52              final DataTransaction dataTran = this.dataStore.begin(false);
53              dataTran.store(KS.RESOURCE, records.get(0));
54              dataTran.store(KS.RESOURCE, records.get(1));
55              dataTran.delete(KS.RESOURCE, records.get(2).getID());
56              dataTran.end(true);
57          } catch (final IOException e) {
58              e.printStackTrace();
59          }
60      }
61  
62      @Test
63      public void testDataLookup() throws Throwable {
64          Stream<Record> stream = null;
65          final DataTransaction dt = this.dataStore.begin(true);
66          try {
67              stream = dt.lookup(KS.RESOURCE, new HashSet<URI>(createURIs(3)), null);
68              Assert.assertNotNull(stream);
69              Assert.assertTrue(stream.iterator().hasNext());
70          } finally {
71              if (stream != null) {
72                  stream.close();
73              }
74              dt.end(true);
75          }
76      }
77  
78      /**
79       * Creates a list of URIs.
80       * 
81       * @param number
82       *            of URIs to be created
83       * @return list of URIs created
84       */
85      protected final List<URI> createURIs(final int number) {
86          final List<URI> uris = new ArrayList<URI>();
87          for (int cont = 0; cont < number; cont++) {
88              final URI idTmp = new URIImpl("http://example.org/" + cont);
89              uris.add(idTmp);
90          }
91          return uris;
92      }
93  
94      /**
95       * Method that creates a list of URI as identifies.
96       * 
97       * @param number
98       *            of URIs to be created
99       * @param type
100      *            the type of record
101      * @return list of URIs created
102      */
103     protected final List<Record> createRecords(final int number, final URI type) {
104         final List<Record> records = new ArrayList<Record>();
105         final List<URI> uris = createURIs(number);
106         for (int cont = 0; cont < number; cont++) {
107             final Record rTmp = Record.create();
108             rTmp.set(RDF.TYPE, type);
109             rTmp.setID(uris.get(cont));
110             records.add(rTmp);
111         }
112         return records;
113     }
114 
115 }