Implemented RestrictedPosition Scrutinizer tests using mocks (#2868)
* Implemented RestrictedPosition Scrutinizer tests using mocks Added RestrictedPositionConstraint class and updated test cases using mocks * Tests updated & working fine
This commit is contained in:
parent
a0a164bb8b
commit
d871a2e151
@ -23,21 +23,48 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.openrefine.wikidata.qa.scrutinizers;
|
package org.openrefine.wikidata.qa.scrutinizers;
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
import org.openrefine.wikidata.qa.QAWarning;
|
import org.openrefine.wikidata.qa.QAWarning;
|
||||||
|
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
|
||||||
|
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.Reference;
|
import org.wikidata.wdtk.datamodel.interfaces.Reference;
|
||||||
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.Statement;
|
||||||
|
import org.wikidata.wdtk.datamodel.interfaces.Value;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class RestrictedPositionScrutinizer extends StatementScrutinizer {
|
public class RestrictedPositionScrutinizer extends StatementScrutinizer {
|
||||||
|
|
||||||
|
public static String SCOPE_CONSTRAINT_QID = "Q53869507";
|
||||||
|
public static String SCOPE_CONSTRAINT_PID = "P5314";
|
||||||
|
public static String SCOPE_CONSTRAINT_VALUE_QID = "Q54828448";
|
||||||
|
public static String SCOPE_CONSTRAINT_QUALIFIER_QID = "Q54828449";
|
||||||
|
public static String SCOPE_CONSTRAINT_REFERENCE_QID = "Q54828450";
|
||||||
|
|
||||||
protected enum SnakPosition {
|
protected enum SnakPosition {
|
||||||
MAINSNAK, QUALIFIER, REFERENCE
|
MAINSNAK, QUALIFIER, REFERENCE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class RestrictedPositionConstraint {
|
||||||
|
boolean isAllowedAsValue, isAllowedAsQualifier, isAllowedAsReference;
|
||||||
|
RestrictedPositionConstraint(Statement statement) {
|
||||||
|
List<SnakGroup> specs = statement.getClaim().getQualifiers();
|
||||||
|
if (specs != null) {
|
||||||
|
ItemIdValue targetValue = Datamodel.makeWikidataItemIdValue(SCOPE_CONSTRAINT_VALUE_QID);
|
||||||
|
ItemIdValue targetQualifier = Datamodel.makeWikidataItemIdValue(SCOPE_CONSTRAINT_QUALIFIER_QID);
|
||||||
|
ItemIdValue targetReference = Datamodel.makeWikidataItemIdValue(SCOPE_CONSTRAINT_REFERENCE_QID);
|
||||||
|
List<Value> snakValues = _fetcher.findValues(specs, SCOPE_CONSTRAINT_PID);
|
||||||
|
isAllowedAsValue = snakValues.contains(targetValue);
|
||||||
|
isAllowedAsQualifier = snakValues.contains(targetQualifier);
|
||||||
|
isAllowedAsReference = snakValues.contains(targetReference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
|
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
|
||||||
// Skip the main snak
|
// Skip the main snak
|
||||||
@ -72,12 +99,16 @@ public class RestrictedPositionScrutinizer extends StatementScrutinizer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean positionAllowed(PropertyIdValue pid, SnakPosition position) {
|
public boolean positionAllowed(PropertyIdValue pid, SnakPosition position) {
|
||||||
if(position.equals(SnakPosition.MAINSNAK)) {
|
List<Statement> constraintDefinitions = _fetcher.getConstraintsByType(pid, SCOPE_CONSTRAINT_QID);
|
||||||
return _fetcher.allowedAsValue(pid);
|
if (!constraintDefinitions.isEmpty()) {
|
||||||
} else if(position.equals(SnakPosition.QUALIFIER)) {
|
RestrictedPositionConstraint constraint = new RestrictedPositionConstraint(constraintDefinitions.get(0));
|
||||||
return _fetcher.allowedAsQualifier(pid);
|
if (position.equals(SnakPosition.MAINSNAK)) {
|
||||||
} else if(position.equals(SnakPosition.REFERENCE)) {
|
return constraint.isAllowedAsValue;
|
||||||
return _fetcher.allowedAsReference(pid);
|
} else if (position.equals(SnakPosition.QUALIFIER)) {
|
||||||
|
return constraint.isAllowedAsQualifier;
|
||||||
|
} else if (position.equals(SnakPosition.REFERENCE)) {
|
||||||
|
return constraint.isAllowedAsReference;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -23,22 +23,41 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.openrefine.wikidata.qa.scrutinizers;
|
package org.openrefine.wikidata.qa.scrutinizers;
|
||||||
|
|
||||||
import java.util.Collections;
|
import org.openrefine.wikidata.qa.ConstraintFetcher;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.openrefine.wikidata.qa.MockConstraintFetcher;
|
|
||||||
import org.openrefine.wikidata.testing.TestingData;
|
import org.openrefine.wikidata.testing.TestingData;
|
||||||
|
import org.openrefine.wikidata.updates.ItemUpdate;
|
||||||
|
import org.openrefine.wikidata.updates.ItemUpdateBuilder;
|
||||||
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.Snak;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
|
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.Statement;
|
import org.wikidata.wdtk.datamodel.interfaces.Statement;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
|
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.openrefine.wikidata.qa.scrutinizers.RestrictedPositionScrutinizer.SCOPE_CONSTRAINT_PID;
|
||||||
|
import static org.openrefine.wikidata.qa.scrutinizers.RestrictedPositionScrutinizer.SCOPE_CONSTRAINT_QID;
|
||||||
|
import static org.openrefine.wikidata.qa.scrutinizers.RestrictedPositionScrutinizer.SCOPE_CONSTRAINT_QUALIFIER_QID;
|
||||||
|
import static org.openrefine.wikidata.qa.scrutinizers.RestrictedPositionScrutinizer.SCOPE_CONSTRAINT_REFERENCE_QID;
|
||||||
|
import static org.openrefine.wikidata.qa.scrutinizers.RestrictedPositionScrutinizer.SCOPE_CONSTRAINT_VALUE_QID;
|
||||||
|
|
||||||
public class RestrictedPositionScrutinizerTest extends SnakScrutinizerTest {
|
public class RestrictedPositionScrutinizerTest extends SnakScrutinizerTest {
|
||||||
|
|
||||||
private ItemIdValue qid = TestingData.existingId;
|
public static PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P22");
|
||||||
|
public static PropertyIdValue mainSnakPid = Datamodel.makeWikidataPropertyIdValue("P1234");
|
||||||
|
public static PropertyIdValue propertyScopeParameter = Datamodel.makeWikidataPropertyIdValue("P5314");
|
||||||
|
public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue(SCOPE_CONSTRAINT_QID);
|
||||||
|
public static ItemIdValue asMainSnak = Datamodel.makeWikidataItemIdValue(SCOPE_CONSTRAINT_VALUE_QID);
|
||||||
|
public static ItemIdValue asQualifier = Datamodel.makeWikidataItemIdValue(SCOPE_CONSTRAINT_QUALIFIER_QID);
|
||||||
|
public static ItemIdValue asReference = Datamodel.makeWikidataItemIdValue(SCOPE_CONSTRAINT_REFERENCE_QID);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EditScrutinizer getScrutinizer() {
|
public EditScrutinizer getScrutinizer() {
|
||||||
@ -47,31 +66,77 @@ public class RestrictedPositionScrutinizerTest extends SnakScrutinizerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTriggerMainSnak() {
|
public void testTriggerMainSnak() {
|
||||||
scrutinize(TestingData.generateStatement(qid, MockConstraintFetcher.qualifierPid, qid));
|
ItemIdValue idA = TestingData.existingId;
|
||||||
|
Snak mainSnak = Datamodel.makeSomeValueSnak(propertyIdValue);
|
||||||
|
Statement statement = new StatementImpl("P22", mainSnak, idA);
|
||||||
|
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||||
|
|
||||||
|
Snak qualifierSnak = Datamodel.makeValueSnak(propertyScopeParameter, asQualifier);
|
||||||
|
List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
|
||||||
|
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
|
||||||
|
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||||
|
when(fetcher.getConstraintsByType(propertyIdValue, SCOPE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||||
|
when(fetcher.findValues(constraintQualifiers, SCOPE_CONSTRAINT_PID)).thenReturn(Collections.singletonList(asQualifier));
|
||||||
|
setFetcher(fetcher);
|
||||||
|
|
||||||
|
scrutinize(update);
|
||||||
assertWarningsRaised("property-found-in-mainsnak");
|
assertWarningsRaised("property-found-in-mainsnak");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoProblem() {
|
public void testNoProblem() {
|
||||||
scrutinize(TestingData.generateStatement(qid, MockConstraintFetcher.mainSnakPid, qid));
|
ItemIdValue idA = TestingData.existingId;
|
||||||
|
Snak mainSnak = Datamodel.makeSomeValueSnak(propertyIdValue);
|
||||||
|
Statement statement = new StatementImpl("P22", mainSnak, idA);
|
||||||
|
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||||
|
|
||||||
|
Snak qualifierSnak = Datamodel.makeValueSnak(propertyScopeParameter, asMainSnak);
|
||||||
|
List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
|
||||||
|
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
|
||||||
|
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||||
|
when(fetcher.getConstraintsByType(propertyIdValue, SCOPE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||||
|
when(fetcher.findValues(constraintQualifiers, SCOPE_CONSTRAINT_PID)).thenReturn(Collections.singletonList(asMainSnak));
|
||||||
|
setFetcher(fetcher);
|
||||||
|
|
||||||
|
scrutinize(update);
|
||||||
assertNoWarningRaised();
|
assertNoWarningRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNotRestricted() {
|
public void testNotRestricted() {
|
||||||
scrutinize(TestingData.generateStatement(qid, Datamodel.makeWikidataPropertyIdValue("P3748"), qid));
|
ItemIdValue idA = TestingData.existingId;
|
||||||
|
Snak mainSnak = Datamodel.makeSomeValueSnak(propertyIdValue);
|
||||||
|
Statement statement = new StatementImpl("P22", mainSnak, idA);
|
||||||
|
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||||
|
|
||||||
|
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||||
|
when(fetcher.getConstraintsByType(propertyIdValue, SCOPE_CONSTRAINT_QID)).thenReturn(new ArrayList<>());
|
||||||
|
setFetcher(fetcher);
|
||||||
|
|
||||||
|
scrutinize(update);
|
||||||
assertNoWarningRaised();
|
assertNoWarningRaised();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTriggerReference() {
|
public void testTriggerReference() {
|
||||||
Snak snak = Datamodel.makeValueSnak(MockConstraintFetcher.mainSnakPid, qid);
|
ItemIdValue idA = TestingData.existingId;
|
||||||
|
Snak snak = Datamodel.makeValueSnak(propertyIdValue, idA);
|
||||||
List<SnakGroup> snakGroups = Collections
|
List<SnakGroup> snakGroups = Collections
|
||||||
.singletonList(Datamodel.makeSnakGroup(Collections.singletonList(snak)));
|
.singletonList(Datamodel.makeSnakGroup(Collections.singletonList(snak)));
|
||||||
Statement statement = Datamodel.makeStatement(
|
Statement statement = Datamodel.makeStatement(
|
||||||
TestingData.generateStatement(qid, MockConstraintFetcher.mainSnakPid, qid).getClaim(),
|
TestingData.generateStatement(idA, propertyIdValue, idA).getClaim(),
|
||||||
Collections.singletonList(Datamodel.makeReference(snakGroups)), StatementRank.NORMAL, "");
|
Collections.singletonList(Datamodel.makeReference(snakGroups)), StatementRank.NORMAL, "");
|
||||||
scrutinize(statement);
|
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||||
|
|
||||||
|
Snak qualifierSnak = Datamodel.makeValueSnak(propertyScopeParameter, asMainSnak);
|
||||||
|
List<SnakGroup> constraintQualifiers = makeSnakGroupList(qualifierSnak);
|
||||||
|
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
|
||||||
|
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||||
|
when(fetcher.getConstraintsByType(propertyIdValue, SCOPE_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||||
|
when(fetcher.findValues(constraintQualifiers, SCOPE_CONSTRAINT_PID)).thenReturn(Collections.singletonList(asMainSnak));
|
||||||
|
setFetcher(fetcher);
|
||||||
|
|
||||||
|
scrutinize(update);
|
||||||
assertWarningsRaised("property-found-in-reference");
|
assertWarningsRaised("property-found-in-reference");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user