Skip to content

Commit

Permalink
If max cardinality is 1 and we do not have a value, return null
Browse files Browse the repository at this point in the history
  • Loading branch information
agarciadom committed Jan 17, 2025
1 parent 86cfa22 commit ec16536
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public Object getProperty(String property, IEolContext context) {
}
}

//return value.isEmpty() ? null : value; // Null returns cause issues with PropertyGetter which expects empty collections for no property matches
return value;
return value.isEmpty() ? null : value;
}

protected Collection<Object> convertLiteralsToValues(Collection<Object> value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

:whiteBoxZX a :Computer ;
:bundle :actionPack ;
:motherBoard :nForce,
:unknownMB .
:motherBoard :nForce .

:actionPack a :GameBundle .

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@
********************************************************************************/
package org.eclipse.epsilon.emc.rdf;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Collection;

import org.eclipse.epsilon.common.util.StringProperties;
import org.eclipse.epsilon.eol.exceptions.models.EolModelLoadingException;
Expand All @@ -35,7 +33,8 @@ public class RDFModelOWLReasonerTest {
private static final String LANGUAGE_PREFERENCE_EN_STRING = "en";
private static final String URI_BIGNAME42 = "urn:x-hp:eg/bigName42";
private static final String URI_ALIENBOX51 = "urn:x-hp:eg/alienBox51";

private static final String URI_WHITEBOX = "urn:x-hp:eg/whiteBoxZX";

private RDFModel model;
private EolContext context;

Expand All @@ -55,7 +54,7 @@ public void loadModelDefaultDataSchemaLangPref() throws EolModelLoadingException
@Test
public void getMotherBoardTest() {
loadModelDefaults();
RDFResource element = model.getElementById(URI_BIGNAME42);
RDFResource element = model.getElementById(URI_WHITEBOX);
Object motherBoard = element.getProperty("eg:motherBoard", context);
assertTrue("motherBoard has max cardinality of 1 should only have that value returned ",
motherBoard instanceof RDFResource);
Expand All @@ -64,25 +63,33 @@ public void getMotherBoardTest() {
// TODO Review this test with Antonio.
// Proposed test: need a similar test to getMotherBoard but for the scenario where null should be returned (i.e. you have no motherboard)
// Getting an empty list back, because there is not motherboard there is also no max cardinality which could be evaluated to 1 and thus a single value or null.

@Test
public void getPropertyThatDoesNotExistAsNullTest() {
loadModelDefaults();
RDFResource element = model.getElementById(URI_ALIENBOX51);
Object motherBoard = element.getProperty("eg:motherBoard", context);
Collection<RDFResource> listMotherBoards = (Collection <RDFResource>) motherBoard;
//assertTrue("URI_ALIENBOX51 computer does not have motherBoard " + motherBoard, motherBoard == null);
assertTrue("URI_ALIENBOX51 computer does not have motherBoard ", listMotherBoards.size() == 0);
assertNull("URI_ALIENBOX51 computer does not have motherBoard ", motherBoard);
}

@Test
public void getMotherBoardTestIssuesWarning() throws IOException {
ByteArrayOutputStream errors = new ByteArrayOutputStream();
System.setErr(new PrintStream(errors));
loadModelDefaults();
RDFResource element = model.getElementById(URI_BIGNAME42);
Object motherBoard = element.getProperty("eg:motherBoard", context);
assertTrue("An error should be raised for max cardinality being raised ", errors.toString().contains("has a max cardinality 1, raw property values list contained"));
ByteArrayOutputStream errors = new ByteArrayOutputStream();

PrintStream oldErr = System.err;
try {
System.setErr(new PrintStream(errors));
loadModelDefaults();

// This will return only the first motherboard, but we actually have two
model.getElementById(URI_BIGNAME42).getProperty("eg:motherBoard", context);

String sErrors = errors.toString();
assertTrue("An error should be raised for max cardinality being exceeded",
sErrors.contains("has a max cardinality 1, raw property values list contained"));
} finally {
System.setErr(oldErr);
}
}

// Functions not tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ public void getAllContentsNamesWithoutPrefixNoPreferredLanguageTags() throws Exc
setupModel(null);
Set<String> names = new HashSet<>();
for (RDFModelElement o : model.allContents()) {
names.addAll((Collection<String>) pGetter.invoke(o, "name", context));
Object oName = pGetter.invoke(o, "name", context);
if (oName instanceof Collection) {
names.addAll((Collection<String>) oName);
}
}
assertEquals("With no language preference and no tag, all values are returned", ALL_NAMES, names);;
}
Expand Down Expand Up @@ -205,7 +208,10 @@ public void getNamesWithoutPrefixAndNoTag() throws Exception {
setupModel(LANGUAGE_PREFERENCE_JA_STRING);
Set<String> names = new HashSet<>();
for (RDFModelElement o : model.allContents()) {
names.addAll((Collection<String>) pGetter.invoke(o, "name@", context));
Object oName = pGetter.invoke(o, "name@", context);
if (oName instanceof Collection) {
names.addAll((Collection<String>) oName);
}
}
assertEquals(ALL_NAMES_UNTAGGED, names);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public void listAll() throws EolModelLoadingException {
public void getNamesWithoutPrefix() throws Exception {
Set<String> names = new HashSet<>();
for (RDFModelElement o : model.allContents()) {
names.addAll((Collection<String>) pGetter.invoke(o, "name", context));
Object nameResult = pGetter.invoke(o, "name", context);
if (nameResult instanceof Collection) {
names.addAll((Collection<String>) nameResult);
}
}
assertEquals("With no language preference and no tag, all values are returned", ALL_NAMES, names);
}
Expand Down

0 comments on commit ec16536

Please sign in to comment.