diff --git a/src/test/java/chatbot/lib/api/dbpedia/TestGenesisService.java b/src/test/java/chatbot/lib/api/dbpedia/TestGenesisService.java index 5e05d97..dc46712 100644 --- a/src/test/java/chatbot/lib/api/dbpedia/TestGenesisService.java +++ b/src/test/java/chatbot/lib/api/dbpedia/TestGenesisService.java @@ -8,22 +8,129 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /** * Created by ramgathreya on 6/21/17. + * Extended with comprehensive test coverage for similar and related entity scenarios. */ public class TestGenesisService { + + /** + * Test retrieving similar entities for a well-known political figure + * Similar entities should be of the same type/category + */ @Test public void checkSimilar() throws Exception { String uris = new GenesisService(0).getSimilarEntities("http://dbpedia.org/resource/Barack_Obama"); - assertNotNull(uris); - assertNotEquals(uris.trim(), ""); + assertNotNull("Similar entities should be retrievable", uris); + assertNotEquals("Response should not be empty", "", uris.trim()); } + /** + * Test retrieving related entities for a well-known political figure + * Related entities should be conceptually connected + */ @Test public void checkRelated() throws Exception { String uris = new GenesisService(0).getRelatedEntities("http://dbpedia.org/resource/Barack_Obama"); - assertNotNull(uris); - assertNotEquals(uris.trim(), ""); + assertNotNull("Related entities should be retrievable", uris); + assertNotEquals("Response should not be empty", "", uris.trim()); + } + + /** + * Test similar entities for a different entity type (country) + * Verifies similar entity retrieval works across entity types + */ + @Test + public void checkSimilarDifferentType() throws Exception { + String uris = new GenesisService(0).getSimilarEntities("http://dbpedia.org/resource/Germany"); + assertNotNull("Similar entities should be retrievable for countries", uris); + } + + /** + * Test related entities for a different entity type (country) + * Verifies related entity retrieval works across entity types + */ + @Test + public void checkRelatedDifferentType() throws Exception { + String uris = new GenesisService(0).getRelatedEntities("http://dbpedia.org/resource/Germany"); + assertNotNull("Related entities should be retrievable for countries", uris); + } + + /** + * Test similar entities for a movie + * Verifies similar entity retrieval works for entertainment entities + */ + @Test + public void checkSimilarMovie() throws Exception { + String uris = new GenesisService(0).getSimilarEntities("http://dbpedia.org/resource/The_Matrix"); + assertNotNull("Similar movies should be retrievable", uris); + } + + /** + * Test related entities for a movie + * Verifies related entity retrieval works for entertainment entities + */ + @Test + public void checkRelatedMovie() throws Exception { + String uris = new GenesisService(0).getRelatedEntities("http://dbpedia.org/resource/The_Matrix"); + assertNotNull("Related movies should be retrievable", uris); + } + + /** + * Test similar entities for a scientific/technical entity + * Verifies similar entity retrieval works for specialized domains + */ + @Test + public void checkSimilarTechnology() throws Exception { + String uris = new GenesisService(0).getSimilarEntities("http://dbpedia.org/resource/Python_(programming_language)"); + assertNotNull("Similar programming languages should be retrievable", uris); + } + + /** + * Test response format contains valid URI references + * Verifies that similar entities are formatted as URIs + */ + @Test + public void checkSimilarResponseFormat() throws Exception { + String uris = new GenesisService(0).getSimilarEntities("http://dbpedia.org/resource/Angela_Merkel"); + assertNotNull("Response should contain URIs", uris); + assertTrue("Response should contain DBpedia URIs in angle brackets", + uris.contains(" 0 for disambiguation pages + assertTrue("Mercury disambiguation should have multiple entries", count > 0); + } + + /** + * Test entity retrieval from disambiguation pages + */ + @Test + public void testDisambiguatedEntityRetrieval() { + if (sparql == null) return; + + String mercuryUri = "http://dbpedia.org/resource/Mercury_(disambiguation)"; + ArrayList entities = sparql.getDisambiguatedEntities(mercuryUri, 0, 5); + + assertNotNull("Should return entity list from disambiguation page", entities); + assertTrue("Should retrieve at least one entity from disambiguation", entities.size() > 0); + + // Verify each entity has necessary information + for (ResponseData entity : entities) { + assertNotNull("Entity should have a title", entity.getTitle()); + assertTrue("Entity title should not be empty", entity.getTitle().length() > 0); + } + } + /** + * Test entity information with all optional fields + */ + @Test + public void testEntityInformationWithOptionalFields() { + if (sparql == null) return; + + String franceUri = "http://dbpedia.org/resource/France"; + ResponseData entity = sparql.getEntityInformation(franceUri); + + assertNotNull("Entity information should be retrieved", entity); + assertNotNull("Entity should have title", entity.getTitle()); + // France should have a description + assertNotNull("Entity should have descriptive text", entity.getText()); + assertTrue("Text content should be substantial", entity.getText().length() > 0); + } + + /** + * Test entity label retrieval and accuracy + */ + @Test + public void testEntityLabelRetrieval() { + if (sparql == null) return; + + String obamaUri = "http://dbpedia.org/resource/Barack_Obama"; + String label = sparql.getLabel(obamaUri); + + assertNotNull("Label should be retrieved", label); + assertEquals("Label should match expected entity name", "Barack Obama", label); + } + + /** + * Test batch entity retrieval by multiple URIs + */ + @Test + public void testBatchEntityRetrieval() { + if (sparql == null) return; + + String uris = " " + + " " + + ""; + + ArrayList entities = sparql.getEntitiesByURIs(uris); + + assertNotNull("Should retrieve multiple entities", entities); + assertEquals("Should retrieve exactly 3 entities", 3, entities.size()); + } + + /** + * Test pagination handling for large disambiguation pages + */ + @Test + public void testDisambiguationPagination() { + if (sparql == null) return; + + String mercuryUri = "http://dbpedia.org/resource/Mercury_(disambiguation)"; + + ArrayList firstPage = sparql.getDisambiguatedEntities(mercuryUri, 0, 3); + ArrayList secondPage = sparql.getDisambiguatedEntities(mercuryUri, 3, 3); + + assertNotNull("First page should exist", firstPage); + + if (firstPage.size() > 0 && secondPage != null && secondPage.size() > 0) { + // Verify pagination is working by comparing first entries + String firstPageFirstTitle = firstPage.get(0).getTitle(); + String secondPageFirstTitle = secondPage.get(0).getTitle(); + assertTrue("Pages should contain different entities", + !firstPageFirstTitle.equals(secondPageFirstTitle)); + } + } + + /** + * Test entity information with minimal data (when some fields are unavailable) + */ + @Test + public void testEntityInformationWithMinimalData() { + if (sparql == null) return; + + // Lesser-known but valid DBpedia entity + String entityUri = "http://dbpedia.org/resource/John_Doe"; + ResponseData entity = sparql.getEntityInformation(entityUri); + + // Should handle gracefully even if some optional fields are missing + if (entity != null) { + assertNotNull("Entity should at least have a title", entity.getTitle()); + } + } + + /** + * Test distinction between disambiguation pages and regular entities + */ + @Test + public void testDisambiguationVsRegularEntity() { + if (sparql == null) return; + + // Test disambiguation page + String disambigUri = "http://dbpedia.org/resource/Mercury_(disambiguation)"; + int disambigCount = sparql.isDisambiguationPage(disambigUri); + + // Test regular entity + String regularUri = "http://dbpedia.org/resource/Mercury_(planet)"; + int regularCount = sparql.isDisambiguationPage(regularUri); + + // Disambiguation should have count > 0, regular entity should be 0 + assertTrue("Disambiguation page should have entities", disambigCount > 0); + assertEquals("Regular entity should not be marked as disambiguation", 0, regularCount); + } + + /** + * Test handling of entity URIs with special characters and encodings + */ + @Test + public void testEntityURIWithSpecialCharacters() { + if (sparql == null) return; + + // Entity with parentheses in name that requires proper URI encoding + String uri = "http://dbpedia.org/resource/Mercury_(planet)"; + ResponseData entity = sparql.getEntityInformation(uri); + + if (entity != null) { + assertNotNull("Entity should be retrievable despite special characters", entity.getTitle()); + } } } diff --git a/src/test/java/rivescript/dbpedia/TestDisambiguation.java b/src/test/java/rivescript/dbpedia/TestDisambiguation.java new file mode 100644 index 0000000..29c7efa --- /dev/null +++ b/src/test/java/rivescript/dbpedia/TestDisambiguation.java @@ -0,0 +1,171 @@ +package rivescript.dbpedia; + +import chatbot.lib.Constants; +import chatbot.lib.TestUtility; +import chatbot.lib.api.SPARQL; +import chatbot.lib.response.ResponseData; +import com.cloudant.client.api.CloudantClient; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Test cases for entity disambiguation, related and similar entity scenarios + */ +public class TestDisambiguation { + private static SPARQL sparql; + + @BeforeClass + public static void setUp() throws Exception { + try { + sparql = new SPARQL(TestUtility.getHelper().getExplorerDB()); + } catch (Exception e) { + // If test utilities fail (missing credentials), tests will be skipped + } + } + + /** + * Test that we correctly identify when an entity is a disambiguation page + */ + @Test + public void testIsDisambiguationPage() { + if (sparql == null) return; + + // "Mercury" is a classic disambiguation case with multiple meanings + String mercuryUri = "http://dbpedia.org/resource/Mercury_(disambiguation)"; + int count = sparql.isDisambiguationPage(mercuryUri); + + // A disambiguation page should have multiple entities + assertTrue("Disambiguation page should have entities", count > 0); + } + + /** + * Test that we don't incorrectly flag non-disambiguation pages + */ + @Test + public void testNonDisambiguationPage() { + if (sparql == null) return; + + // Barack Obama is a specific person, not a disambiguation page + String obamaUri = "http://dbpedia.org/resource/Barack_Obama"; + int count = sparql.isDisambiguationPage(obamaUri); + + // Should return 0 for non-disambiguation pages + assertEquals("Non-disambiguation page should return 0", 0, count); + } + + /** + * Test retrieving entities from a disambiguation page + */ + @Test + public void testGetDisambiguatedEntities() { + if (sparql == null) return; + + String mercuryUri = "http://dbpedia.org/resource/Mercury_(disambiguation)"; + ArrayList entities = sparql.getDisambiguatedEntities(mercuryUri, 0, 10); + + assertNotNull("Should return entity list", entities); + assertTrue("Disambiguation page should have multiple entities", entities.size() > 0); + } + + /** + * Test retrieving entity information with complete details + */ + @Test + public void testGetEntityInformation() { + if (sparql == null) return; + + String obamaUri = "http://dbpedia.org/resource/Barack_Obama"; + ResponseData entity = sparql.getEntityInformation(obamaUri); + + assertNotNull("Entity information should not be null", entity); + assertNotNull("Entity should have a title", entity.getTitle()); + assertTrue("Entity title should not be empty", entity.getTitle().length() > 0); + } + + /** + * Test retrieving multiple entities by their URIs + */ + @Test + public void testGetEntitiesByURIs() { + if (sparql == null) return; + + String uris = " "; + ArrayList entities = sparql.getEntitiesByURIs(uris); + + assertNotNull("Should return entity list", entities); + assertEquals("Should return exactly 2 entities", 2, entities.size()); + } + + /** + * Test entity label retrieval + */ + @Test + public void testGetLabel() { + if (sparql == null) return; + + String obamaUri = "http://dbpedia.org/resource/Barack_Obama"; + String label = sparql.getLabel(obamaUri); + + assertNotNull("Label should not be null", label); + assertEquals("Label should match expected name", "Barack Obama", label); + } + + /** + * Test pagination in disambiguation results + */ + @Test + public void testDisambiguationPagination() { + if (sparql == null) return; + + String mercuryUri = "http://dbpedia.org/resource/Mercury_(disambiguation)"; + + ArrayList page1 = sparql.getDisambiguatedEntities(mercuryUri, 0, 5); + ArrayList page2 = sparql.getDisambiguatedEntities(mercuryUri, 5, 5); + + assertNotNull("First page should exist", page1); + // Verify different entities in pagination + if (page2 != null && page1.size() > 0 && page2.size() > 0) { + assertNotNull("Second page should exist", page2); + assertNotEquals("Pages should be different", page1.get(0).getTitle(), page2.get(0).getTitle()); + } + } + + /** + * Test entity with description and abstract fields + */ + @Test + public void testEntityDescriptionAndAbstract() { + if (sparql == null) return; + + String franceUri = "http://dbpedia.org/resource/France"; + ResponseData entity = sparql.getEntityInformation(franceUri); + + assertNotNull("Entity should exist", entity); + assertNotNull("Entity should have text content", entity.getText()); + assertTrue("Text should contain meaningful information", entity.getText().length() > 20); + } + + /** + * Test handling of entities with special characters in names + */ + @Test + public void testEntityWithSpecialCharacters() { + if (sparql == null) return; + + // Test with an entity that has parentheses in the name + String uri = "http://dbpedia.org/resource/Mercury_(planet)"; + ResponseData entity = sparql.getEntityInformation(uri); + + assertNotNull("Entity with special characters should be retrievable", entity); + if (entity != null) { + assertNotNull("Entity should have a title", entity.getTitle()); + } + } +}