Implemented ResctrictedValue Scrutinizer tests using mocks (#2877)
* Implemented RestrictedValueScrutinizer tests using mocks Added AllowedValueConstraint & DisallowedValueConstraint classes & updated the test cases too. * Tests updated
This commit is contained in:
parent
3717111db8
commit
a0a164bb8b
@ -1,25 +1,64 @@
|
|||||||
package org.openrefine.wikidata.qa.scrutinizers;
|
package org.openrefine.wikidata.qa.scrutinizers;
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.openrefine.wikidata.qa.QAWarning;
|
import org.openrefine.wikidata.qa.QAWarning;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.Snak;
|
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 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 class RestrictedValuesScrutinizer extends SnakScrutinizer {
|
||||||
|
|
||||||
public static String type = "forbidden-value";
|
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<Value> allowedValues;
|
||||||
|
AllowedValueConstraint(Statement statement) {
|
||||||
|
List<SnakGroup> specs = statement.getClaim().getQualifiers();
|
||||||
|
if (specs != null) {
|
||||||
|
List<Value> properties = _fetcher.findValues(specs, ALLOWED_VALUES_CONSTRAINT_PID);
|
||||||
|
allowedValues = properties.stream().collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DisallowedValueConstraint {
|
||||||
|
Set<Value> disallowedValues;
|
||||||
|
DisallowedValueConstraint(Statement statement) {
|
||||||
|
List<SnakGroup> specs = statement.getClaim().getQualifiers();
|
||||||
|
if (specs != null) {
|
||||||
|
List<Value> properties = _fetcher.findValues(specs, DISALLOWED_VALUES_CONSTRAINT_PID);
|
||||||
|
disallowedValues = properties.stream().collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
|
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
|
||||||
PropertyIdValue pid = snak.getPropertyId();
|
PropertyIdValue pid = snak.getPropertyId();
|
||||||
Value value = snak.getValue();
|
Value value = snak.getValue();
|
||||||
|
List<Statement> allowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, ALLOWED_VALUES_CONSTRAINT_QID);
|
||||||
Set<Value> allowedValues = _fetcher.allowedValues(pid);
|
List<Statement> disallowedValueConstraintDefinitions = _fetcher.getConstraintsByType(pid, DISALLOWED_VALUES_CONSTRAINT_QID);
|
||||||
Set<Value> disallowedValues = _fetcher.disallowedValues(pid);
|
Set<Value> 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)) ||
|
if((allowedValues != null && !allowedValues.contains(value)) ||
|
||||||
(disallowedValues != null && disallowedValues.contains(value))) {
|
(disallowedValues != null && disallowedValues.contains(value))) {
|
||||||
QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
|
QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
|
||||||
|
@ -1,14 +1,40 @@
|
|||||||
package org.openrefine.wikidata.qa.scrutinizers;
|
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.openrefine.wikidata.testing.TestingData;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
|
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.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 {
|
public class RestrictedValuesScrutinizerTest extends SnakScrutinizerTest {
|
||||||
|
|
||||||
private ItemIdValue qid = Datamodel.makeWikidataItemIdValue("Q3487");
|
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
|
@Override
|
||||||
public EditScrutinizer getScrutinizer() {
|
public EditScrutinizer getScrutinizer() {
|
||||||
@ -17,6 +43,10 @@ public class RestrictedValuesScrutinizerTest extends SnakScrutinizerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoConstraint() {
|
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,
|
scrutinize(TestingData.generateStatement(qid,
|
||||||
Datamodel.makeWikidataPropertyIdValue("P28732"),
|
Datamodel.makeWikidataPropertyIdValue("P28732"),
|
||||||
qid));
|
qid));
|
||||||
@ -25,33 +55,73 @@ public class RestrictedValuesScrutinizerTest extends SnakScrutinizerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAllowedValue() {
|
public void testAllowedValue() {
|
||||||
scrutinize(TestingData.generateStatement(qid,
|
Snak mainSnak = Datamodel.makeValueSnak(allowedPropertyIdValue, allowedValue);
|
||||||
MockConstraintFetcher.allowedValuesPid,
|
Statement statement = new StatementImpl("P1622", mainSnak, qid);
|
||||||
MockConstraintFetcher.allowedValueQid));
|
|
||||||
|
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedValue);
|
||||||
|
List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
|
||||||
|
List<Statement> 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();
|
assertNoWarningRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAllowedValueFailing() {
|
public void testAllowedValueFailing() {
|
||||||
scrutinize(TestingData.generateStatement(qid,
|
Snak mainSnak = Datamodel.makeSomeValueSnak(allowedPropertyIdValue);
|
||||||
MockConstraintFetcher.allowedValuesPid,
|
Statement statement = new StatementImpl("P1622", mainSnak, qid);
|
||||||
qid));
|
|
||||||
|
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedValue);
|
||||||
|
List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
|
||||||
|
List<Statement> 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);
|
assertWarningsRaised(RestrictedValuesScrutinizer.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDisallowedValue() {
|
public void testDisallowedValue() {
|
||||||
scrutinize(TestingData.generateStatement(qid,
|
Snak mainSnak = Datamodel.makeSomeValueSnak(disallowedPropertyIdValue);
|
||||||
MockConstraintFetcher.forbiddenValuesPid,
|
Statement statement = new StatementImpl("P31", mainSnak, qid);
|
||||||
qid));
|
|
||||||
|
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, disallowedValue);
|
||||||
|
List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
|
||||||
|
List<Statement> 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();
|
assertNoWarningRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDisallowedValueFailing() {
|
public void testDisallowedValueFailing() {
|
||||||
scrutinize(TestingData.generateStatement(qid,
|
Snak mainSnak = Datamodel.makeValueSnak(disallowedPropertyIdValue, disallowedValue);
|
||||||
MockConstraintFetcher.forbiddenValuesPid,
|
Statement statement = new StatementImpl("P31", mainSnak, qid);
|
||||||
MockConstraintFetcher.forbiddenValueQid));
|
|
||||||
|
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, disallowedValue);
|
||||||
|
List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
|
||||||
|
List<Statement> 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);
|
assertWarningsRaised(RestrictedValuesScrutinizer.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user