Search in sources :

Example 11 with ValueFactory

use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.

the class ValueFactoryTest method testText.

/**
     * Internally used to create and text {@link Text}s for the different tests
     * 
     * @param textString
     *            the natural language text as string
     * @param language
     *            the language
     * @return the created {@link Text} instance that can be used to perform further tests.
     */
private Text testText(String textString, String language) {
    ValueFactory vf = getValueFactory();
    Text text = vf.createText(textString, language);
    assertNotNull(text.getText());
    assertNotNull(text.getText());
    assertEquals(text.getText(), textString);
    if (language == null) {
        assertTrue(text.getLanguage() == null);
    } else if (language.isEmpty()) {
        // implementations are free to change an empty language string to null
        // NOTE that it is not allowed to change NULL to an empty String!
        assertTrue(text.getLanguage() == null || text.getLanguage().isEmpty());
    } else {
        assertNotNull(text.getLanguage());
        assertEquals(text.getLanguage(), language);
    }
    return text;
}
Also used : Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 12 with ValueFactory

use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.

the class ValueFactoryTest method testRepresentation.

private Representation testRepresentation(String id) {
    ValueFactory vf = getValueFactory();
    Representation rep = vf.createRepresentation(id);
    assertNotNull(rep);
    assertNotNull(rep.getId());
    if (id != null) {
        assertEquals(rep.getId(), id);
    }
    return rep;
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Example 13 with ValueFactory

use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.

the class RepresentationTest method testStringArrayToTextConversion.

/**
     * Checks if {@link Text} instances are correctly generated for String[]. This test depends also on the
     * correct implementation of the {@link Text#equals(Object)} method
     */
@Test
public void testStringArrayToTextConversion() {
    String field = "urn:the.field:used.for.this.Test";
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    // test conversion of String[] with language as second element
    String[] textWithLang = new String[] { "Test text with language", "en" };
    rep.add(field, textWithLang);
    Iterator<Text> refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    Text test = refs.next();
    assertEquals(textWithLang[1], test.getLanguage());
    assertEquals(textWithLang[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, textWithLang);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createText(textWithLang[0], textWithLang[1]));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, textWithLang);
    assertFalse(rep.get(field).hasNext());
    // test conversion of String[] with only one element (default language)
    String[] textWithoutLang = new String[] { "Test text without language" };
    rep.add(field, textWithoutLang);
    refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    test = refs.next();
    assertNull(test.getLanguage());
    assertEquals(textWithoutLang[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, textWithoutLang);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createText(textWithoutLang[0]));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, textWithoutLang);
    assertFalse(rep.get(field).hasNext());
    // test conversion of String[] with null as second element (default language)
    String[] textWithDefaultLang = new String[] { "Test text with default language", null };
    rep.add(field, textWithDefaultLang);
    refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    test = refs.next();
    assertNull(test.getLanguage());
    assertEquals(textWithDefaultLang[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, textWithDefaultLang);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createText(textWithDefaultLang[0], null));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, textWithDefaultLang);
    assertFalse(rep.get(field).hasNext());
    // finally test if additional Elements are correctly ignored
    String[] ignoreAdditionalElements = new String[] { "Test if additional elements are ignored", "en", "ignored1", "ignored2", null, "ignored4" };
    String[] sameText = new String[] { "Test if additional elements are ignored", "en" };
    rep.add(field, ignoreAdditionalElements);
    refs = rep.get(field, (String[]) null);
    assertTrue(refs.hasNext());
    test = refs.next();
    assertEquals(ignoreAdditionalElements[1], test.getLanguage());
    assertEquals(ignoreAdditionalElements[0], test.getText());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, ignoreAdditionalElements);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test if an Array with only the first two elements generate the same Text
    rep.add(field, sameText);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, ignoreAdditionalElements);
    assertFalse(rep.get(field).hasNext());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) Test(org.junit.Test)

Example 14 with ValueFactory

use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.

the class RepresentationTest method testURLToReferenceConversion.

/**
     * Tests if {@link Reference} instances are correctly generated for {@link URL}. This test also depends on
     * the correct implementation of the {@link Reference#equals(Object)} method
     * 
     * @throws MalformedURLException
     */
@Test
public void testURLToReferenceConversion() throws MalformedURLException {
    String field = "urn:the.field:used.for.this.Test";
    URL url = new URL("http://www.test.org/urlTest");
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    // test empty reference
    Iterator<Reference> refs = rep.getReferences(field);
    assertFalse(refs.hasNext());
    // test conversion
    rep.add(field, url);
    refs = rep.getReferences(field);
    assertTrue(refs.hasNext());
    assertEquals(refs.next().getReference(), url.toString());
    assertFalse(refs.hasNext());
    // test multiple adds do not generate duplicate References
    rep.add(field, url);
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test adding a equivalent reference
    rep.add(field, vf.createReference(url.toString()));
    assertTrue(asCollection(rep.get(field)).size() == 1);
    // test removing
    rep.remove(field, url);
    assertFalse(rep.get(field).hasNext());
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URL(java.net.URL) Test(org.junit.Test)

Example 15 with ValueFactory

use of org.apache.stanbol.entityhub.servicesapi.model.ValueFactory in project stanbol by apache.

the class ClerezzaYardTest method testRemovalOfTypeRepresentationStatement.

/**
     * The Clerezza Yard uses the Statement<br>
     * <code>representationId -> rdf:type -> Representation</code><br>
     * to identify that an IRI in the RDF graph (Graph) represents a
     * Representation. This Triple is added when a Representation is stored and
     * removed if retrieved from the Yard.<p>
     * This tests if this functions as expected
     * @throws YardException
     */
@Test
public void testRemovalOfTypeRepresentationStatement() throws YardException {
    Yard yard = getYard();
    ValueFactory vf = yard.getValueFactory();
    Reference representationType = vf.createReference(RdfResourceEnum.Representation.getUri());
    Representation test = create();
    //the rdf:type Representation MUST NOT be within the Representation
    Assert.assertFalse(test.get(RDF.type.getUnicodeString()).hasNext());
    //now add the statement and see if an IllegalStateException is thrown
    /*
         * The triple within this Statement is internally used to "mark" the
         * URI of the Representation as 
         */
    test.add(RDF.type.getUnicodeString(), representationType);
}
Also used : Yard(org.apache.stanbol.entityhub.servicesapi.yard.Yard) Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) Test(org.junit.Test) YardTest(org.apache.stanbol.entityhub.test.yard.YardTest)

Aggregations

ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)17 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)7 Test (org.junit.Test)7 URI (java.net.URI)4 HashSet (java.util.HashSet)4 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)4 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)4 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)3 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)3 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)3 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)3 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)3 URL (java.net.URL)2 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)2 AdaptingIterator (org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)2 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)2 StringReader (java.io.StringReader)1