From a0a164bb8b8d626b06ebb36bdfe125d9c82330ef Mon Sep 17 00:00:00 2001 From: Ekta Mishra Date: Sat, 4 Jul 2020 17:53:52 +0530 Subject: [PATCH] Implemented ResctrictedValue Scrutinizer tests using mocks (#2877) * Implemented RestrictedValueScrutinizer tests using mocks Added AllowedValueConstraint & DisallowedValueConstraint classes & updated the test cases too. * Tests updated --- .../RestrictedValuesScrutinizer.java | 49 +++++++++- .../RestrictedValuesScrutinizerTest.java | 96 ++++++++++++++++--- 2 files changed, 127 insertions(+), 18 deletions(-) diff --git a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizer.java b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizer.java index 5aed7b8b4..734c41066 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizer.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizer.java @@ -1,25 +1,64 @@ package org.openrefine.wikidata.qa.scrutinizers; -import java.util.Set; - import org.openrefine.wikidata.qa.QAWarning; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.Snak; +import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; +import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Value; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + public class RestrictedValuesScrutinizer extends SnakScrutinizer { public static String type = "forbidden-value"; + public static String ALLOWED_VALUES_CONSTRAINT_QID = "Q21510859"; + public static String ALLOWED_VALUES_CONSTRAINT_PID = "P2305"; + + public static String DISALLOWED_VALUES_CONSTRAINT_QID = "Q52558054"; + public static String DISALLOWED_VALUES_CONSTRAINT_PID = "P2305"; + + class AllowedValueConstraint { + Set allowedValues; + AllowedValueConstraint(Statement statement) { + List specs = statement.getClaim().getQualifiers(); + if (specs != null) { + List properties = _fetcher.findValues(specs, ALLOWED_VALUES_CONSTRAINT_PID); + allowedValues = properties.stream().collect(Collectors.toSet()); + } + } + } + + class DisallowedValueConstraint { + Set disallowedValues; + DisallowedValueConstraint(Statement statement) { + List specs = statement.getClaim().getQualifiers(); + if (specs != null) { + List properties = _fetcher.findValues(specs, DISALLOWED_VALUES_CONSTRAINT_PID); + disallowedValues = properties.stream().collect(Collectors.toSet()); + } + } + } @Override public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { PropertyIdValue pid = snak.getPropertyId(); Value value = snak.getValue(); - - Set allowedValues = _fetcher.allowedValues(pid); - Set disallowedValues = _fetcher.disallowedValues(pid); + List allowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, ALLOWED_VALUES_CONSTRAINT_QID); + List disallowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, DISALLOWED_VALUES_CONSTRAINT_QID); + Set allowedValues = null, disallowedValues = null; + if (!allowedValueConstraintDefinitions.isEmpty()) { + AllowedValueConstraint constraint = new AllowedValueConstraint(allowedValueConstraintDefinitions.get(0)); + allowedValues = constraint.allowedValues; + } + if (!disallowedValueConstraintDefinitions.isEmpty()) { + DisallowedValueConstraint constraint = new DisallowedValueConstraint(disallowedValueConstraintDefinitions.get(0)); + disallowedValues = constraint.disallowedValues; + } if((allowedValues != null && !allowedValues.contains(value)) || (disallowedValues != null && disallowedValues.contains(value))) { QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1); diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizerTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizerTest.java index 5ba9b58eb..52539e5fa 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizerTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/RestrictedValuesScrutinizerTest.java @@ -1,14 +1,40 @@ package org.openrefine.wikidata.qa.scrutinizers; -import org.openrefine.wikidata.qa.MockConstraintFetcher; +import org.openrefine.wikidata.qa.ConstraintFetcher; import org.openrefine.wikidata.testing.TestingData; import org.testng.annotations.Test; import org.wikidata.wdtk.datamodel.helpers.Datamodel; +import org.wikidata.wdtk.datamodel.implementation.StatementImpl; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; +import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; +import org.wikidata.wdtk.datamodel.interfaces.Snak; +import org.wikidata.wdtk.datamodel.interfaces.SnakGroup; +import org.wikidata.wdtk.datamodel.interfaces.Statement; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.openrefine.wikidata.qa.scrutinizers.RestrictedValuesScrutinizer.ALLOWED_VALUES_CONSTRAINT_PID; +import static org.openrefine.wikidata.qa.scrutinizers.RestrictedValuesScrutinizer.ALLOWED_VALUES_CONSTRAINT_QID; +import static org.openrefine.wikidata.qa.scrutinizers.RestrictedValuesScrutinizer.DISALLOWED_VALUES_CONSTRAINT_PID; +import static org.openrefine.wikidata.qa.scrutinizers.RestrictedValuesScrutinizer.DISALLOWED_VALUES_CONSTRAINT_QID; public class RestrictedValuesScrutinizerTest extends SnakScrutinizerTest { private ItemIdValue qid = Datamodel.makeWikidataItemIdValue("Q3487"); + public static PropertyIdValue allowedPropertyIdValue = Datamodel.makeWikidataPropertyIdValue("P1622"); + public static ItemIdValue allowedValue = Datamodel.makeWikidataItemIdValue("Q13196750"); + public static PropertyIdValue disallowedPropertyIdValue = Datamodel.makeWikidataPropertyIdValue("P31"); + public static ItemIdValue disallowedValue = Datamodel.makeWikidataItemIdValue("Q47"); + public static ItemIdValue allowedValueEntity = Datamodel.makeWikidataItemIdValue(ALLOWED_VALUES_CONSTRAINT_QID); + public static ItemIdValue disallowedValueEntity = Datamodel.makeWikidataItemIdValue(DISALLOWED_VALUES_CONSTRAINT_QID); + + public static PropertyIdValue itemParameterPID = Datamodel.makeWikidataPropertyIdValue("P2305"); @Override public EditScrutinizer getScrutinizer() { @@ -17,6 +43,10 @@ public class RestrictedValuesScrutinizerTest extends SnakScrutinizerTest { @Test public void testNoConstraint() { + ConstraintFetcher fetcher = mock(ConstraintFetcher.class); + when(fetcher.getConstraintsByType(any(), eq(ALLOWED_VALUES_CONSTRAINT_QID))).thenReturn(new ArrayList<>()); + when(fetcher.getConstraintsByType(any(), eq(DISALLOWED_VALUES_CONSTRAINT_QID))).thenReturn(new ArrayList<>()); + setFetcher(fetcher); scrutinize(TestingData.generateStatement(qid, Datamodel.makeWikidataPropertyIdValue("P28732"), qid)); @@ -25,33 +55,73 @@ public class RestrictedValuesScrutinizerTest extends SnakScrutinizerTest { @Test public void testAllowedValue() { - scrutinize(TestingData.generateStatement(qid, - MockConstraintFetcher.allowedValuesPid, - MockConstraintFetcher.allowedValueQid)); + Snak mainSnak = Datamodel.makeValueSnak(allowedPropertyIdValue, allowedValue); + Statement statement = new StatementImpl("P1622", mainSnak, qid); + + Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedValue); + List constraintQualifiers = makeSnakGroupList(qualifierSnak); + List constraintDefinitions = constraintParameterStatementList(allowedValueEntity, constraintQualifiers); + ConstraintFetcher fetcher = mock(ConstraintFetcher.class); + when(fetcher.getConstraintsByType(allowedPropertyIdValue, ALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(constraintDefinitions); + when(fetcher.getConstraintsByType(allowedPropertyIdValue, DISALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(new ArrayList<>()); + when(fetcher.findValues(constraintQualifiers, ALLOWED_VALUES_CONSTRAINT_PID)).thenReturn(Collections.singletonList(allowedValue)); + setFetcher(fetcher); + + scrutinize(statement); assertNoWarningRaised(); } @Test public void testAllowedValueFailing() { - scrutinize(TestingData.generateStatement(qid, - MockConstraintFetcher.allowedValuesPid, - qid)); + Snak mainSnak = Datamodel.makeSomeValueSnak(allowedPropertyIdValue); + Statement statement = new StatementImpl("P1622", mainSnak, qid); + + Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedValue); + List constraintQualifiers = makeSnakGroupList(qualifierSnak); + List constraintDefinitions = constraintParameterStatementList(allowedValueEntity, constraintQualifiers); + ConstraintFetcher fetcher = mock(ConstraintFetcher.class); + when(fetcher.getConstraintsByType(allowedPropertyIdValue, ALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(constraintDefinitions); + when(fetcher.getConstraintsByType(allowedPropertyIdValue, DISALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(new ArrayList<>()); + when(fetcher.findValues(constraintQualifiers, ALLOWED_VALUES_CONSTRAINT_PID)).thenReturn(Collections.singletonList(allowedValue)); + setFetcher(fetcher); + + scrutinize(statement); assertWarningsRaised(RestrictedValuesScrutinizer.type); } @Test public void testDisallowedValue() { - scrutinize(TestingData.generateStatement(qid, - MockConstraintFetcher.forbiddenValuesPid, - qid)); + Snak mainSnak = Datamodel.makeSomeValueSnak(disallowedPropertyIdValue); + Statement statement = new StatementImpl("P31", mainSnak, qid); + + Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, disallowedValue); + List constraintQualifiers = makeSnakGroupList(qualifierSnak); + List constraintDefinitions = constraintParameterStatementList(disallowedValueEntity, constraintQualifiers); + ConstraintFetcher fetcher = mock(ConstraintFetcher.class); + when(fetcher.getConstraintsByType(disallowedPropertyIdValue, ALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(new ArrayList<>()); + when(fetcher.getConstraintsByType(disallowedPropertyIdValue, DISALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(constraintDefinitions); + when(fetcher.findValues(constraintQualifiers, DISALLOWED_VALUES_CONSTRAINT_PID)).thenReturn(Collections.singletonList(disallowedValue)); + setFetcher(fetcher); + + scrutinize(statement); assertNoWarningRaised(); } @Test public void testDisallowedValueFailing() { - scrutinize(TestingData.generateStatement(qid, - MockConstraintFetcher.forbiddenValuesPid, - MockConstraintFetcher.forbiddenValueQid)); + Snak mainSnak = Datamodel.makeValueSnak(disallowedPropertyIdValue, disallowedValue); + Statement statement = new StatementImpl("P31", mainSnak, qid); + + Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, disallowedValue); + List constraintQualifiers = makeSnakGroupList(qualifierSnak); + List constraintDefinitions = constraintParameterStatementList(disallowedValueEntity, constraintQualifiers); + ConstraintFetcher fetcher = mock(ConstraintFetcher.class); + when(fetcher.getConstraintsByType(disallowedPropertyIdValue, ALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(new ArrayList<>()); + when(fetcher.getConstraintsByType(disallowedPropertyIdValue, DISALLOWED_VALUES_CONSTRAINT_QID)).thenReturn(constraintDefinitions); + when(fetcher.findValues(constraintQualifiers, DISALLOWED_VALUES_CONSTRAINT_PID)).thenReturn(Collections.singletonList(disallowedValue)); + setFetcher(fetcher); + + scrutinize(statement); assertWarningsRaised(RestrictedValuesScrutinizer.type); }